Maximum Value and Index (Set 12)

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 12) — the data. This set runs 4 cases: nums holds arrays of 46–48 items, with values from -50 to 89. The hidden cases are the largest of the set.

Example 1

maxValueAndIndex()
Input
nums = [-20, 50, 39, -5, -47, 63, 70, 59, 3, 22, -10, 57, 64, 16, 64, -20, 73, -14, 64, 48, 22, 82, -41, 89, -26, -17, -37, 34, 20, 66, 63, 47, 9, -37, -31, 4, 89, 66, 18, 42, 67, -49, 72, -41, 70, -42]
Output
[89, 23]

Why

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

Example 2

maxValueAndIndex()
Input
nums = [18, -12, 58, 16, -37, 36, -3, 21, 63, -25, 85, -33, -20, 77, 15, 50, 14, 69, 64, 22, 6, -48, 77, -23, 57, 64, 72, -1, 55, -40, -15, 12, 2, 19, 89, 73, -10, -31, 26, -47, -34, 75, 68, 1, -39, -25, 75, 82]
Output
[89, 34]

Why

`maxValueAndIndex` returns `[89, 34]` 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.