BackeasyArrays

Parity-Indexed Extremes Solution

Problem Statement

Given a 0-indexed integer array nums, identify the maximum element located at the even indices and the minimum element located at the odd indices. Return these results as a two-element array [max_even, min_odd].

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

Explanation: Step-by-step: With input [2, 9, 1, 4, 5, 3, 7, 6, 8, 0, 1], we first identify the maximum element at even indices. The even indices are 0, 2, 4, 6, 8, 10. The maximum element at these indices is 9. Next, we identify the minimum element at odd indices. The odd indices are 1, 3, 5, 7, 9. The minimum element at these indices is 1. Therefore, the output is [9, 1].

Example 2
Input
[40, 20, 30, 10, 50, 60, 70, 80, 90, 100, 110]
Output
[110, 10]

Explanation: Step-by-step: With input [40, 20, 30, 10, 50, 60, 70, 80, 90, 100, 110], we first identify the maximum element at even indices. The even indices are 0, 2, 4, 6, 8, 10. The maximum element at these indices is 110. Next, we identify the minimum element at odd indices. The odd indices are 1, 3, 5, 7, 9. The minimum element at these indices is 10. Therefore, the output is [110, 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

Parity-Indexed Extremes — Problem Statement & Solution Guide

ArraysEasyBasic Traversal
TimeO(n)
|
SpaceO(1)

Problem Description

Given a 0-indexed integer array nums, identify the maximum element located at the even indices and the minimum element located at the odd indices. Return these results as a two-element array [max_even, min_odd].

DSA Pattern Breakdown

DSA Pattern Breakdown

"Parity-Indexed Extremes"

easy

WHY DOES IT MATTER?

This pattern teaches candidates how to extract multiple statistics from a single data stream, a skill crucial for performance‑critical code where extra passes are prohibitive.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that index parity can be evaluated in O(1) per element, allowing simultaneous updates of two aggregates without auxiliary storage.

REAL-WORLD CONNECTION

Think of monitoring a distributed log where even‑indexed entries represent heartbeat signals and odd‑indexed entries represent error codes; you need the strongest heartbeat (max) and the weakest error signal (min) in one sweep to trigger alerts.

During an interview, write the two trackers upfront, update them inside a single for‑loop, and immediately handle edge cases (empty array, single‑element array) before the loop to avoid sentinel bugs.

COMPLEXITY AT A GLANCE

⏱ Time:O(n)
💾 Space:O(1)

Core Theory — Why This Approach?

The problem asks for two separate aggregates: the maximum value among elements whose indices are even, and the minimum value among elements whose indices are odd. A naïve solution would scan the array twice—once for each parity—resulting in O(2n) time, which is still linear but incurs extra passes and potential cache inefficiency. More importantly, a naïve approach might mistakenly treat the parity of the values rather than the indices, leading to incorrect results on large inputs where the distinction matters. The optimal paradigm leverages a single linear pass, maintaining two running variables: one for the current maximum at even positions and one for the current minimum at odd positions. By updating these variables in constant time per element, we achieve O(n) time with O(1) auxiliary space, which scales gracefully even for arrays with millions of entries.

From an algorithmic perspective, this pattern exemplifies the "single‑pass reduction" technique, where multiple aggregates are computed simultaneously while iterating. It avoids the overhead of multiple traversals and eliminates the need for auxiliary data structures like separate lists for even and odd indices. The key insight is that the parity of an index can be determined with a simple bitwise AND (i & 1) or modulo operation, allowing us to branch the update logic efficiently. This approach is robust against edge cases such as arrays containing only even‑indexed elements or only odd‑indexed elements, provided we initialize the trackers with appropriate sentinel values (e.g., Integer.MIN_VALUE for max and Integer.MAX_VALUE for min).

Interview Questions on This Problem

Q1How would you modify the solution if the requirement changed to find the sum of even‑indexed elements and the product of odd‑indexed elements?

Initialize sumEven = 0 and prodOdd = 1. During a single pass, if index is even add nums[i] to sumEven; if odd multiply prodOdd by nums[i] (taking care of overflow and zero handling). The time remains O(n) and space O(1).

Q2Can you solve the problem in a functional programming style without explicit loops?

Yes. In languages like JavaScript or Python, you can use filter with enumerate to separate even and odd indexed elements, then apply max() and min() respectively. Though conceptually a single pass, the underlying implementation still traverses the array twice, so time is O(n) but with higher constant factors.

Q3What would be the impact on time and space complexity if the array were stored on a distributed system where each node holds a chunk of the array?

Each node can compute local maxEven and minOdd in O(chunkSize) time and O(1) space. A final reduction step aggregates the global maxEven (taking max of locals) and global minOdd (taking min of locals) in O(numberOfNodes) time and O(1) extra space, preserving overall linear scalability.

Examples

Example 1

Input

[2, 9, 1, 4, 5, 3, 7, 6, 8, 0, 1]

Output

[9, 1]

Explanation: Step-by-step: With input [2, 9, 1, 4, 5, 3, 7, 6, 8, 0, 1], we first identify the maximum element at even indices. The even indices are 0, 2, 4, 6, 8, 10. The maximum element at these indices is 9. Next, we identify the minimum element at odd indices. The odd indices are 1, 3, 5, 7, 9. The minimum element at these indices is 1. Therefore, the output is [9, 1].

Example 2

Input

[40, 20, 30, 10, 50, 60, 70, 80, 90, 100, 110]

Output

[110, 10]

Explanation: Step-by-step: With input [40, 20, 30, 10, 50, 60, 70, 80, 90, 100, 110], we first identify the maximum element at even indices. The even indices are 0, 2, 4, 6, 8, 10. The maximum element at these indices is 110. Next, we identify the minimum element at odd indices. The odd indices are 1, 3, 5, 7, 9. The minimum element at these indices is 10. Therefore, the output is [110, 10].

Constraints

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

Optimal Approach & Strategy

The optimal solution iterates once, updating two scalar variables on the fly: maxEven for even positions and minOdd for odd positions. This achieves O(n) time with O(1) extra space.

Brute Force Approach

A naive solution would create two separate lists—one for even indices and one for odd indices—then compute max on the first list and min on the second. This requires extra O(n) space and two passes over the data.

Verified Code Solutions

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

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.