✏️ Explanatory Question

[Arrays]

Question:

What is the length of the array int arr[], if its first index is x and the last index is y?

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

Answer:

Length = y - x + 1

Explanation:

Array length is calculated using:

Last Index - First Index + 1

Here:

  • First index = x
  • Last index = y

Therefore:

Length = y - x + 1

Example:

If:

First index = 3
Last index = 7

Then:

Length = 7 - 3 + 1
       = 5

The array elements are:

arr[3], arr[4], arr[5], arr[6], arr[7]

Total elements = 5

Conclusion:

  • Array length depends on first and last index
  • Formula used:
    Length = Last Index - First Index + 1