Maximum Value and Index (Set 6)

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

Example 1

maxValueAndIndex()
Input
nums = [-42, -48, 68, 33, -31, -7, 72, 24, 3, 68, 25, 89, -1, 25, 80, 69, 55, -18, 26, -44, -7, -47, -49, 18, -15, -11, 0, -28, -42, 15, -29, -14, 61, -14, 56, 2, 62, -47, 62, 36, 59, 35, 10, -46, 36, -13]
Output
[89, 11]

Why

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

Example 2

maxValueAndIndex()
Input
nums = [51, 79, 3, -24, 59, -23, 21, 82, 8, -25, 61, 85, 89, -18, 49, -45, -50, 44, 48, 57, 31, 56, -9]
Output
[89, 12]

Why

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