Problem overview
The method middleElement receives an array of integers, nums, with an odd number of items, and returns the item in the middle. For [1, 2, 3] that is 2. For [5, 9, 1, 8, 4] it is 1: the middle is about the position of an item, not the size of its value. For [7] the only item is the middle one.
Approach
With an odd number of items, there is exactly one item with the same number of items before it and after it. With 5 items, the indexes are 0 to 4, and index 2 has two items on each side. The index is the length divided by 2, rounded down: 5 / 2 is 2.5, and rounded down it is 2.
Rounding down matters because division in TypeScript doesn't produce a whole number. The type number is a decimal type, so 7 / 2 is 3.5, and an array can't be read at the index 2.5: nums[2.5] is undefined. Math.floor rounds a number down to the nearest whole number and fixes that.
Step by step
- Find the middle index. Divide
nums.lengthby 2 and round down withMath.floor. Store the result in a constantmiddle, declared withconstand the typenumber. - Return the item at that index:
nums[middle].
The solution in TypeScript
class Solution {
middleElement(nums: number[]): number {
const middle: number = Math.floor(nums.length / 2);
return nums[middle];
}
}
Some notes on the code:
- Division gives a decimal. For 5 items,
nums.length / 2is 2.5. Reading the array at that position givesundefined, so the rounding step is required. Math.floorrounds toward negative infinity. For a length, which is never negative,Math.trunc, which drops the decimal part, gives the same result. The two differ only for negative numbers:Math.floor(-2.5)is -3 andMath.trunc(-2.5)is -2.- Named constant. Putting the index in
middlegives the step a name and keeps thereturnline short.
Why integer division needs help
Some languages divide two integers and give an integer. TypeScript doesn't: every number is a double, and / always gives the exact quotient, so 5 / 2 is 2.5 and 1 / 2 is 0.5. Whenever an index comes from a division, round it explicitly, as the lesson does.
Tracing the example
| nums | length | length / 2 | middle | result |
|---|---|---|---|---|
[1, 2, 3] | 3 | 1.5 | 1 | 2 |
[5, 9, 1, 8, 4] | 5 | 2.5 | 2 | 1 |
[7] | 1 | 0.5 | 0 | 7 |
[-3, 0, 8, 2, 6, 10, 4] | 7 | 3.5 | 3 | 2 |
In each row, the number of items before the middle index equals the number after it.
Edge cases and common mistakes
- Skipping the rounding.
nums[nums.length / 2]reads index 2.5 for 5 items and givesundefined. - Subtracting 1 by habit. The last index is
length - 1, but the middle index isMath.floor(length / 2). For 5 items the middle index is 2, not 1 or 3. - Returning the middle index. The task asks for the item at that position. For
[5, 9, 1, 8, 4]the answer is1, not2. - Picking the middle by value. The middle is a position. The median would need a sorted array, which this lesson doesn't ask for.
- Even lengths. The lesson only gives odd lengths. With an even number of items there are two middle positions, and this code would return the later of them.
Testing
[1, 2, 3]gives2.[5, 9, 1, 8, 4]gives1.[7]gives7.[-3, 0, 8, 2, 6, 10, 4]gives2.
