Maximum Value and Index (Set 8)

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 8) — the data. This set runs 4 cases: nums holds arrays of 35–43 items, with values from -49 to 90. The hidden cases are the largest of the set.

Example 1

maxValueAndIndex()
Input
nums = [39, 3, 14, 17, -2, 51, 2, -1, -29, 17, -28, -13, 35, 7, -40, 77, -25, 54, 36, 31, 80, -13, -28, 10, 52, 67, 10, 49, -38, -39, 84, 13, -5, 40, -9, 42, -21, 54, 30, 71, 86, -31]
Output
[86, 40]

Why

`maxValueAndIndex` returns `[86, 40]` for this input.

Example 2

maxValueAndIndex()
Input
nums = [30, -18, 59, 57, -36, -15, -9, 43, 26, 90, -5, -39, 83, -46, 20, 68, 1, 64, 14, 32, 84, -17, 74, -31, 21, 47, -46, 1, 84, -48, -43, -37, -8, 42, -33, 43, -49, -40]
Output
[90, 9]

Why

`maxValueAndIndex` returns `[90, 9]` 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.