BackeasyArraysAmazon

Even-Odd Extremes Difference Solution

Problem Statement

Given an integer array nums of size at least 2, find the absolute difference between the maximum value located at an even index and the minimum value located at an odd index.

Example 1
Input
[5, 2, 9, 1, 7, 3]
Output
8

Explanation: Step-by-step: with input [5, 2, 9, 1, 7, 3], we first identify the maximum value at an even index, which is 9 (at index 2), and the minimum value at an odd index, which is 1 (at index 3). The absolute difference between these two values is |9 - 1| = 8.

Example 2
Input
[1, -1, 3, 5, 7, 9]
Output
10

Explanation: Step-by-step: with input [1, -1, 3, 5, 7, 9], we first identify the maximum value at an even index, which is 9 (at index 5), and the minimum value at an odd index, which is -1 (at index 1). The absolute difference between these two values is |9 - (-1)| = 10.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Even-Odd Extremes Difference — Problem Statement & Solution Guide

ArraysEasyBasic Traversal
TimeO(n)
|
SpaceO(1)

Problem Description

Given an integer array nums of size at least 2, find the absolute difference between the maximum value located at an even index and the minimum value located at an odd index.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Even-Odd Extremes Difference"

easy

WHY DOES IT MATTER?

This pattern reduces memory overhead by avoiding auxiliary data structures, which is crucial when dealing with large datasets or streaming data. It also simplifies the code, making it easier to reason about correctness and performance.

OPTIMIZATION CHALLENGE

The insight is that extrema can be updated incrementally without storing all elements, turning a potentially O(n) space solution into an O(1) space one.

REAL-WORLD CONNECTION

In distributed log aggregation, you often need to find the maximum latency per server and the minimum latency per client in a single pass over a massive log stream, mirroring this single-pass extrema pattern.

When explaining this to an interviewer, emphasize the importance of state variables and how they enable constant space, and be ready to discuss edge cases like all even or all odd indices.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
đź’ľ Space:O(1)

Core Theory — Why This Approach?

The problem asks for the absolute difference between two specific extrema: the maximum value among elements at even indices and the minimum value among elements at odd indices. A naive solution would involve two nested loops or separate scans that collect all even-indexed and odd-indexed values into auxiliary arrays, then compute the extrema, leading to O(n) time but also O(n) additional space. However, the optimal approach requires only a single pass through the array, maintaining two running variables—one for the current maximum at even indices and one for the current minimum at odd indices—thereby achieving O(n) time with O(1) extra space.

The key algorithmic insight is that the extrema can be updated incrementally: when encountering an element at an even index, compare it with the current even-max and update if larger; similarly, when encountering an odd index, compare with the current odd-min and update if smaller. This eliminates the need to store all values, which is especially important for large inputs where memory usage becomes a bottleneck. The final absolute difference is then computed in constant time after the single traversal.

This pattern exemplifies the classic “single-pass scan with running aggregates” paradigm, which is ubiquitous in interview questions involving extrema, prefix sums, or sliding windows. It demonstrates how careful state management can reduce both time and space complexity, a skill that is highly valued in production systems where performance and resource constraints are critical.

Interview Questions on This Problem

Q1How would you modify the algorithm if the array could contain negative numbers and you needed the difference between the largest even-indexed value and the smallest odd-indexed value?

The algorithm remains unchanged; the comparison operators handle negative values naturally. You still track the maximum at even indices and the minimum at odd indices, then compute the absolute difference.

Q2A fintech platform asks: if the array represents transaction amounts over days, how could you extend this solution to also return the indices of the max even and min odd values?

Maintain two additional variables to store the indices alongside the extrema values. Update them whenever you update the corresponding value during the single pass.

Q3During a high-growth startup interview, you’re asked: what if the array is extremely large and streamed in chunks? How would you adapt the solution?

Process each chunk sequentially, updating the running maximum and minimum as you go. Since you only need the final extrema, you can discard processed chunks, keeping the space usage constant.

Examples

Example 1

Input

[5, 2, 9, 1, 7, 3]

Output

8

Explanation: Step-by-step: with input [5, 2, 9, 1, 7, 3], we first identify the maximum value at an even index, which is 9 (at index 2), and the minimum value at an odd index, which is 1 (at index 3). The absolute difference between these two values is |9 - 1| = 8.

Example 2

Input

[1, -1, 3, 5, 7, 9]

Output

10

Explanation: Step-by-step: with input [1, -1, 3, 5, 7, 9], we first identify the maximum value at an even index, which is 9 (at index 5), and the minimum value at an odd index, which is -1 (at index 1). The absolute difference between these two values is |9 - (-1)| = 10.

Constraints

  • 2 <= nums.length <= 10^5
  • -10^9 <= nums[i] <= 10^9

Optimal Approach & Strategy

Traverse the array once, updating two running variables for the even-index maximum and odd-index minimum. After the traversal, compute the absolute difference of these two values.

Brute Force Approach

Collect all even-indexed elements into one list and all odd-indexed elements into another, then compute the max of the first list and the min of the second list, finally take their absolute difference. This requires two passes and extra memory.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function solution(nums) { let maxEven = -Infinity, minOdd = Infinity; for (let i = 0; i < nums.length; i++) { if (i % 2 === 0 && nums[i] > maxEven) maxEven = nums[i]; if (i % 2 !== 0 && nums[i] < minOdd) minOdd = nums[i]; } return Math.abs(maxEven - minOdd); }

Asked in Top Tech Interviews

Amazon

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.