Maximum Value and Index (Set 9)

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

Example 1

maxValueAndIndex()
Input
nums = [30, 24, 74, 17, 62, -28, 36, 13, 2, 18, -15, 13, 73, 15, -43, -37, -25, 31, -34, -7, 32, -6, -15, -39, 73, 16, 21, -24, -31, 79, 43, -47, 0, 52, 86, 86, 82, -42]
Output
[86, 34]

Why

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

Example 2

maxValueAndIndex()
Input
nums = [23, 64, -41, -40, 46, 56, 33, -7, -48, -44, -18, 78, 74, -25, 41, -29, -15, -30, 83, -5, 74, -3, 44, -34, 10, -44, 1, -39, 9, 65, -10, 34, 26, 36, 29, -31, 84, -45, 67, 89, -13, -34]
Output
[89, 39]

Why

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