Maximum Value and Index (Set 14)

Easy

Given a non-empty array of integers nums, return [value, index] for the largest value. On a tie, return the first index.

Maximum Value and Index (Set 14) — the data. This set runs 4 cases: nums holds arrays of 48–48 items, with values from -50 to 90. The hidden cases are the largest of the set.

Example 1

maxValueAndIndex()
Input
nums = [-20, 1, 18, 24, 53, 52, 20, 7, 2, -27, -5, 52, -5, 54, -50, 56, -36, 58, 35, -11, -39, 8, 4, 87, 45, -6, -27, 57, 26, 34, 16, 35, -41, -41, -17, 79, 28, -21, 45, -46, 25, 45, 42, 51, -20, 24, -2, 45]
Output
[87, 23]

Why

`maxValueAndIndex` returns `[87, 23]` for this input.

Example 2

maxValueAndIndex()
Input
nums = [47, -25, 76, 44, 50, -20, -50, 67, 5, 85, -49, 1, 30, 80, 74, 88, -19, 23, 4, 11, 69, 75, 65, 55, 77, 10, -16, -21, 38, 18, 78, 63, -50, 42, -25, -29, 74, 1, 35, 75, -26, -19, 66, 17, 49, 3, 41, 58]
Output
[88, 15]

Why

`maxValueAndIndex` returns `[88, 15]` for this input.
Constraints
1 <= nums.length <= 10^5
-10^5 <= nums[i] <= 10^5
Every case in this set must pass: the first two are visible on Run, the last two unlock on Submit.

Topics

Arrays

Implement maxValueAndIndex in the editor and return the answer — printing it is never graded.