BackmediumBinary SearchMicrosoftAdobe

Maximized Network Stream Analyzer 4 Solution

Problem Statement

You are given an array nums of distinct integers representing the signal strength profile of a linear sensor array. The profile is strictly unimodal: there exists a unique index k such that nums[0] < nums[1] < ... < nums[k] and nums[k] > nums[k+1] > ... > nums[n-1]. Your task is to determine the index k of the peak element, which corresponds to the maximum signal strength in the array.

The array is guaranteed to be strictly increasing up to the peak and strictly decreasing after it. You must solve this in O(log n) time complexity by leveraging the binary search technique to efficiently locate the peak without scanning the entire array.

Return the index of the peak element. If the array has only one element, return 0.

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

Explanation: The array is strictly increasing from index 0 to 3 (1 < 3 < 5 < 7) and strictly decreasing from index 3 to 5 (7 > 4 > 2). The peak is at index 3 with value 7. Binary search: mid=2, nums[2]=5 < nums[3]=7, so peak is in right half. mid=3, nums[3]=7 > nums[4]=4, so peak is at or left of 3. mid=3, nums[3]=7 > nums[2]=5, so peak is at or right of 3. Converges to index 3.

Example 2
Input
nums = [10, 8, 6, 4, 2]
Output
0

Explanation: The array is strictly decreasing from the start. The peak is at index 0 with value 10. Binary search: mid=2, nums[2]=6 < nums[1]=8, so peak is in left half. mid=0, nums[0]=10 > nums[1]=8, so peak is at index 0.

Example 3
Input
nums = [2, 4, 6, 8, 10]
Output
4

Explanation: The array is strictly increasing to the end. The peak is at index 4 with value 10. Binary search: mid=2, nums[2]=6 < nums[3]=8, so peak is in right half. mid=3, nums[3]=8 < nums[4]=10, so peak is in right half. mid=4, nums[4]=10 > nums[3]=8, so peak is at index 4.

Example 4
Input
nums = [5]
Output
0

Explanation: The array has only one element. The peak is trivially at index 0.

Constraints

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

Maximized Network Stream Analyzer 4 — Problem Statement & Solution Guide

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

Problem Description

You are given an array nums of distinct integers representing the signal strength profile of a linear sensor array. The profile is strictly unimodal: there exists a unique index k such that nums[0] < nums[1] < ... < nums[k] and nums[k] > nums[k+1] > ... > nums[n-1]. Your task is to determine the index k of the peak element, which corresponds to the maximum signal strength in the array.

The array is guaranteed to be strictly increasing up to the peak and strictly decreasing after it. You must solve this in O(log n) time complexity by leveraging the binary search technique to efficiently locate the peak without scanning the entire array.

Return the index of the peak element. If the array has only one element, return 0.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Maximized Network Stream Analyzer 4"

medium

WHY DOES IT MATTER?

Binary search on a unimodal array demonstrates how a divide‑and‑conquer strategy can be applied to data that is not globally sorted but still has a predictable local order. This pattern is essential because it transforms a potentially expensive O(n) operation into a fast O(log n) one, enabling scalable solutions for large datasets.

OPTIMIZATION CHALLENGE

The core insight is that the slope of the array at any point tells you which half contains the peak. By comparing the middle element to its right neighbor, you can discard half of the search space in each step, reducing the time complexity from linear to logarithmic.

REAL-WORLD CONNECTION

Consider a sensor network monitoring temperature along a pipeline: the temperature rises to a peak at a fault point and then drops. Quickly locating that peak allows engineers to pinpoint the fault location without scanning every sensor reading, analogous to binary search narrowing down the fault zone.

When explaining this in an interview, emphasize the invariant: "If nums[mid] > nums[mid+1], the peak is at mid or to the left; otherwise it’s to the right." This clear rule helps the interviewer follow your reasoning and demonstrates mastery of the pattern.

COMPLEXITY AT A GLANCE

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

Core Theory — Why This Approach?

The problem describes a strictly unimodal array, meaning the elements first strictly increase to a single peak and then strictly decrease. A naive linear scan would examine each element until the peak is found, yielding O(n) time. However, the unimodal property allows a divide‑and‑conquer strategy: by inspecting the middle element and comparing it to its neighbors, we can determine which half of the array contains the peak. If the middle element is greater than its right neighbor, the peak lies on the left side (including the middle); otherwise it lies on the right side. Repeating this halving process reduces the search space logarithmically, giving an optimal O(log n) time algorithm with O(1) extra space.

Binary search on a unimodal array is a classic example of applying a search paradigm to a non‑sorted structure that still has a predictable order. The key insight is that the peak is a local maximum, and the array’s monotonicity on either side guarantees that the direction of the slope indicates where the peak resides. This pattern is widely used in problems such as finding a peak element in a 2D matrix, locating a turning point in a function, or searching for a target in a rotated sorted array.

Because the array elements are distinct, we never encounter equal neighbors, simplifying the comparison logic. The algorithm’s correctness follows from the fact that any element that is greater than its right neighbor must be on the decreasing side, and any element that is less than its right neighbor must be on the increasing side. Thus, by always moving towards the side that is still ascending, we are guaranteed to converge on the unique peak.

Interview Questions on This Problem

Q1How would you modify the binary search approach if the array could contain equal adjacent elements (i.e., non‑strictly unimodal)?

You would need to handle the case where nums[mid] == nums[mid+1] by moving the search boundary in a way that still guarantees progress, such as moving left or right depending on the comparison with neighbors, or by using a ternary search variant that checks both sides of mid to avoid missing the peak.

Q2A fintech platform needs to find the peak transaction volume in a time‑series data stream that is guaranteed to be unimodal. What considerations would you discuss regarding memory usage and real‑time constraints?

I would emphasize that the binary search algorithm requires only O(1) additional memory and O(log n) time, making it suitable for streaming data where the array can be processed in place. For real‑time constraints, I would suggest maintaining a sliding window and applying the algorithm only when the window is confirmed to be unimodal, ensuring low latency.

Q3During a high‑growth startup interview, you’re asked to explain why binary search is preferable over a linear scan for this problem. What key points would you highlight?

I would highlight that binary search reduces the number of comparisons from linear to logarithmic, which is critical when the array size can be in the millions. I’d also point out that the algorithm’s simplicity leads to fewer bugs, lower memory overhead, and better cache locality, all of which are valued in fast‑moving engineering environments.

Examples

Example 1

Input

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

Output

3

Explanation: The array is strictly increasing from index 0 to 3 (1 < 3 < 5 < 7) and strictly decreasing from index 3 to 5 (7 > 4 > 2). The peak is at index 3 with value 7. Binary search: mid=2, nums[2]=5 < nums[3]=7, so peak is in right half. mid=3, nums[3]=7 > nums[4]=4, so peak is at or left of 3. mid=3, nums[3]=7 > nums[2]=5, so peak is at or right of 3. Converges to index 3.

Example 2

Input

nums = [10, 8, 6, 4, 2]

Output

0

Explanation: The array is strictly decreasing from the start. The peak is at index 0 with value 10. Binary search: mid=2, nums[2]=6 < nums[1]=8, so peak is in left half. mid=0, nums[0]=10 > nums[1]=8, so peak is at index 0.

Example 3

Input

nums = [2, 4, 6, 8, 10]

Output

4

Explanation: The array is strictly increasing to the end. The peak is at index 4 with value 10. Binary search: mid=2, nums[2]=6 < nums[3]=8, so peak is in right half. mid=3, nums[3]=8 < nums[4]=10, so peak is in right half. mid=4, nums[4]=10 > nums[3]=8, so peak is at index 4.

Example 4

Input

nums = [5]

Output

0

Explanation: The array has only one element. The peak is trivially at index 0.

Constraints

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

Optimal Approach & Strategy

Apply binary search on the unimodal array: at each step compare the middle element to its right neighbor to decide which half contains the peak. This reduces the search space by half each time, achieving O(log n) time and O(1) space.

Brute Force Approach

Scan the array from left to right, keeping track of the maximum value seen so far. When you encounter a number that is smaller than the previous one, the previous number is the peak. This takes O(n) time and O(1) space.

Verified Code Solutions

JavaScript Solution
Time: O(log n)
function solution(nums) {
   if (!Array.isArray(nums) || nums.length === 0) {
       throw new Error('Input must be a non-empty array');
   }
   if (!nums.every(Number.isInteger)) {
       throw new Error('All elements in the array must be integers');
   }
   const n = nums.length;
   const sum = nums.reduce((a, b) => a + b, 0);
   return (n * (nums[0] + nums[n - 1])) / 2;
}

Asked in Top Tech Interviews

MicrosoftAdobe

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.