Exit
  1. Learn
  2. Check
  3. Solve
  4. Reflect

Open a box by its number

An array keeps values in a row of numbered boxes. The number of a box is its index, and it starts at 0. To read a box, write the array's name with the index in square brackets: nums[0] is the first value.

That's why reading any box is instant, however long the array is. The computer doesn't search: it goes straight to box 3, the way you'd walk straight to locker 3.

The first box is box 0: the second is box 1.
Any box, instantly: no searching needed.
Step through the indices
7
0
2
1
9
2
4
3
5
4
i

Box 0 holds 7, the first value. In code that's nums[0].

Move 1 of 4

The last box is one less than the length

The length is how many boxes there are. With 5 boxes numbered from 0, the numbers are 0, 1, 2, 3 and 4. So the last box is always the length minus one.

Asking for box 5 in a row of 5 is asking for a locker that isn't there. Depending on the language you'll get an error or a strange value, so it's worth getting this right early.

Last box: the length minus one.
Box number = length: is one past the end.
In code
first = nums[0]
last = nums[len(nums) - 1]
return [first, last]