What is the length of an array when its first index is denoted by i and last…
2021
What is the length of an array when its first index is denoted by i and last index is denoted by j?
- A.
length of an array = i + j
- B.
length of an array = j − i
- C.
length of an array = j − i + 1
- D.
length of an array = j − i − 1
Attempted by 1114 students.
Show answer & explanation
Correct answer: C
To find the length of an array when the first index is i and the last index is j, we need to count the number of elements from i to j, inclusive. The formula is: length = j - i + 1. Explanation: The difference (j - i) gives the number of steps between the indices. Adding 1 accounts for the inclusion of both the first and last elements.