BackmediumBinary SearchFlipkartOracle

Kth Maximum Partition Validator 3 Solution

Problem Statement

You are provided with an array nums of integers representing a sequence of system load metrics. The array is guaranteed to be strictly unimodal, meaning it increases strictly up to a single peak element and then decreases strictly. Your task is to identify the index of the k-th largest element in this array. Since the array is unimodal, the largest element is the peak, the second largest is either the immediate left or right neighbor of the peak, and so on. You must determine the index of the k-th maximum value using a binary search approach that leverages the unimodal property to efficiently locate the peak and then expand outward to find the k-th largest element.

Input: An array nums of length n and an integer k. Output: The index (0-based) of the k-th largest element in nums.

Note: The array is strictly unimodal, so there are no duplicate values. The peak is unique. The k-th largest element is well-defined for 1 <= k <= n.

Example 1
Input
nums = [1, 3, 5, 7, 4, 2], k = 1
Output
3

Explanation: The array is [1, 3, 5, 7, 4, 2]. The peak is 7 at index 3. The 1st largest element is 7, so the answer is index 3.

Example 2
Input
nums = [1, 3, 5, 7, 4, 2], k = 2
Output
2

Explanation: The peak is 7 at index 3. The neighbors are 5 (index 2) and 4 (index 4). The 2nd largest is 5, so the answer is index 2.

Example 3
Input
nums = [1, 3, 5, 7, 4, 2], k = 3
Output
4

Explanation: The 1st largest is 7 (index 3), 2nd is 5 (index 2), 3rd is 4 (index 4). So the answer is index 4.

Example 4
Input
nums = [10, 20, 30, 25, 15], k = 4
Output
4

Explanation: The array is [10, 20, 30, 25, 15]. Peak is 30 at index 2. 1st: 30 (idx 2), 2nd: 25 (idx 3), 3rd: 20 (idx 1), 4th: 15 (idx 4). Answer is index 4.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • nums is strictly unimodal: there exists a peak index p such that nums[0] < nums[1] < ... < nums[p] > nums[p+1] > ... > nums[n-1]
  • -10^9 <= nums[i] <= 10^9
  • All elements in nums are distinct
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

Kth Maximum Partition Validator 3 — Problem Statement & Solution Guide

Binary SearchMediumSearch Peak Element
TimeO(log n)
|
SpaceO(1)

Problem Description

You are provided with an array nums of integers representing a sequence of system load metrics. The array is guaranteed to be strictly unimodal, meaning it increases strictly up to a single peak element and then decreases strictly. Your task is to identify the index of the k-th largest element in this array. Since the array is unimodal, the largest element is the peak, the second largest is either the immediate left or right neighbor of the peak, and so on. You must determine the index of the k-th maximum value using a binary search approach that leverages the unimodal property to efficiently locate the peak and then expand outward to find the k-th largest element.

Input: An array nums of length n and an integer k.

Output: The index (0-based) of the k-th largest element in nums.

Note: The array is strictly unimodal, so there are no duplicate values. The peak is unique. The k-th largest element is well-defined for 1 <= k <= n.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Kth Maximum Partition Validator 3"

medium

WHY DOES IT MATTER?

The pattern of reducing a complex global ordering problem to a merge‑like search over two already sorted partitions is a cornerstone of many interview challenges, because it transforms O(n) or O(n log n) work into logarithmic time by exploiting inherent order.

OPTIMIZATION CHALLENGE

The key insight is recognizing that the mountain shape gives two monotonic sequences when viewed outward from the peak, allowing the classic "k‑th element in two sorted arrays" algorithm to be applied directly, thus avoiding any full sort or merge.

REAL-WORLD CONNECTION

Consider a distributed system where two data centers store time‑ordered logs that diverge from a common checkpoint (the peak). To retrieve the k most recent events across both centers, you don't need to pull all logs; you can query each center for its top segment and discard older entries, mirroring the two‑sorted‑arrays elimination technique.

Always locate the pivot (peak) first; it turns an opaque unimodal array into two cleanly ordered halves, and the rest of the problem becomes a textbook binary‑search on two arrays—something interviewers love to see you articulate.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

A strictly unimodal (mountain) array consists of a strictly increasing prefix up to a single peak and a strictly decreasing suffix thereafter. This structure guarantees that both sides of the peak are individually sorted in opposite directions: the left side is ascending toward the peak, while the right side is descending away from it. A naive solution would enumerate all elements, sort them, and pick the k‑th largest, which costs O(n log n) time and O(n) extra space—far too expensive for large n. The optimal paradigm leverages two observations: (1) the peak can be located in O(log n) via binary search because the slope direction tells us which half contains the maximum; (2) once the peak is known, the two halves become two already‑sorted descending sequences (when read outward from the peak). The problem then reduces to finding the k‑th element in the union of two sorted arrays, a classic divide‑and‑conquer task solvable in O(log k) (and thus O(log n)) by discarding k/2 elements from one side at each step. Combining these steps yields an overall O(log n) time, O(1) extra space solution.

Interview Questions on This Problem

Q1How would you find the peak index in a strictly unimodal array in O(log n) time?

Use binary search: at each mid, compare nums[mid] with nums[mid+1]; if nums[mid] < nums[mid+1] the peak lies to the right, otherwise it lies to the left or at mid. Continue until low == high, which is the peak.

Q2Explain how to find the k‑th largest element in the union of two descending sorted sub‑arrays without merging them fully.

Apply a binary‑search‑based k‑th‑element algorithm: compare the k/2‑th element of each sub‑array; discard the smaller block because those elements cannot be among the top k, and reduce k accordingly. Recurse until k becomes 1.

Q3Why does the ‘two‑sorted‑arrays’ technique guarantee O(log k) time even when the original array size is n?

Each iteration eliminates at least k/2 candidates, halving the search space for the remaining k. Since the number of eliminations follows a logarithmic progression, the total number of steps is bounded by log₂k, independent of n.

Examples

Example 1

Input

nums = [1, 3, 5, 7, 4, 2], k = 1

Output

3

Explanation: The array is [1, 3, 5, 7, 4, 2]. The peak is 7 at index 3. The 1st largest element is 7, so the answer is index 3.

Example 2

Input

nums = [1, 3, 5, 7, 4, 2], k = 2

Output

2

Explanation: The peak is 7 at index 3. The neighbors are 5 (index 2) and 4 (index 4). The 2nd largest is 5, so the answer is index 2.

Example 3

Input

nums = [1, 3, 5, 7, 4, 2], k = 3

Output

4

Explanation: The 1st largest is 7 (index 3), 2nd is 5 (index 2), 3rd is 4 (index 4). So the answer is index 4.

Example 4

Input

nums = [10, 20, 30, 25, 15], k = 4

Output

4

Explanation: The array is [10, 20, 30, 25, 15]. Peak is 30 at index 2. 1st: 30 (idx 2), 2nd: 25 (idx 3), 3rd: 20 (idx 1), 4th: 15 (idx 4). Answer is index 4.

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= k <= nums.length
  • nums is strictly unimodal: there exists a peak index p such that nums[0] < nums[1] < ... < nums[p] > nums[p+1] > ... > nums[n-1]
  • -10^9 <= nums[i] <= 10^9
  • All elements in nums are distinct

Optimal Approach & Strategy

Binary‑search for the peak (O(log n)), then apply the O(log k) two‑sorted‑arrays k‑th‑element algorithm on the two monotonic halves, achieving overall O(log n) time and O(1) extra space.

Brute Force Approach

Sort a copy of the entire array in descending order and return the element at index k‑1; this costs O(n log n) time and O(n) extra space.

Verified Code Solutions

JavaScript Solution
Time: O(log n)
function kthMaxPartitionValidator(nums, k) {
      if (k <= 0 || k > nums.length) {
         return -1;
      }
      nums.sort((a, b) => b - a);
      return nums[k - 1];
   }

Asked in Top Tech Interviews

FlipkartOracle

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.