553.Get the First, Middle and Last Elements
Easy
Given an array
nums, return [first, middle, last], where the middle element is at index length / 2 (integer division).
This is the textbook example of O(1)Constant Time O(1)The work stays the same no matter how large the input is. work. Because array elements are stored next to each other, reading any index takes the same time whether the array has five elements or five million. Walking the array to reach those positions also works, but its cost grows with the length — O(n) work for an O(1) job.
Example 1:
Input: nums = [4,8,15,16,23]
Output: [4,15,23]
Example 2:
Input: nums = [1,2,3,4]
Output: [1,3,4]
+ 4 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ nums.length ≤ 1000 - ●
-1000000 ≤ nums[i] ≤ 1000000
nums =
[4, 8, 15, 16, 23]