BackeasyArrays Amazon

Even-Odd Extremes Difference Solution

Problem Statement

Given an integer array `nums` of size at least 2, find the difference between the maximum value located at an even index and the minimum value located at an odd index. Specifically, return the value of $max\_even - min\_odd$, where $max\_even$ is the maximum value among all elements at indices $0, 2, 4, \dots$ and $min\_odd$ is the minimum value among all elements at indices $1, 3, 5, \dots$.

Example 1
Input
nums = [4, 2, 7, 1, 9]
Output
8

Explanation: The elements at even indices are 4, 7, and 9 (max is 9). The elements at odd indices are 2 and 1 (min is 1). The difference is 9 - 1 = 8.

Example 2
Input
nums = [-3, 5, -1, 10]
Output
-6

Explanation: The elements at even indices are -3 and -1 (max is -1). The elements at odd indices are 5 and 10 (min is 5). The difference is -1 - 5 = -6.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Live Compiler
Loading...
Test Cases & Output
🔒 Sign up to run your code

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free