Peaks in Array - Problem
Peaks in Array is a dynamic range query problem that challenges you to efficiently handle two types of operations on an array.

You are given an integer array nums and need to process a series of queries. A peak is defined as an element that is strictly greater than both its neighboring elements (left and right). Important: The first and last elements of an array cannot be peaks since they don't have two neighbors.

You must handle two types of queries:
โ€ข Type 1: [1, left, right] - Count the number of peaks in the subarray from index left to right (inclusive)
โ€ข Type 2: [2, index, value] - Update nums[index] to value

Return an array containing the results of all Type 1 queries in the order they appear.

Example: In array [1, 4, 3, 8, 5], element 4 at index 1 and element 8 at index 3 are peaks since 1 < 4 > 3 and 3 < 8 > 5.

Input & Output

example_1.py โ€” Basic Peak Counting
$ Input: nums = [1,4,3,8,5], queries = [[2,3,4],[1,0,4]]
โ€บ Output: [2]
๐Ÿ’ก Note: Initially peaks are at indices 1 (4>1,3) and 3 (8>3,5). After update nums[3]=4, we have [1,4,3,4,5]. Query [1,0,4] counts peaks in range [0,4]: index 1 (4>1,3) and index 3 (4>3,5), so answer is 2.
example_2.py โ€” No Peaks in Short Range
$ Input: nums = [1,2,3], queries = [[1,0,2]]
โ€บ Output: [1]
๐Ÿ’ก Note: Array [1,2,3] has one peak at index 1 (2>1, but 2<3 so not a peak). Actually, element 2 at index 1 satisfies 2>1 and we need 2>3 which is false. So no peaks. Wait, let me recalculate: 2>1 (true) and 2>3 (false), so index 1 is not a peak. Answer should be [0].
example_3.py โ€” Multiple Updates
$ Input: nums = [1,2,1,2,1], queries = [[1,1,3],[2,2,3],[1,1,3]]
โ€บ Output: [0,1]
๐Ÿ’ก Note: Initially: [1,2,1,2,1]. Index 1: 2>1,1 โœ“ (peak), Index 3: 2>1,1 โœ“ (peak). Query [1,1,3] asks for peaks in [1,3], which includes indices 1,2 - only index 1 is peak, but we check range [2,2] which has no peaks. After update nums[2]=3: [1,2,3,2,1]. New peaks: index 2 (3>2,2 โœ“). Query [1,1,3] now finds 1 peak at index 2.

Constraints

  • 3 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 105
  • 1 โ‰ค queries.length โ‰ค 105
  • queries[i] = [1, li, ri] or queries[i] = [2, indexi, vali]
  • 0 โ‰ค li โ‰ค ri โ‰ค nums.length - 1
  • 0 โ‰ค indexi โ‰ค nums.length - 1
  • 1 โ‰ค vali โ‰ค 105

Visualization

Tap to expand
Peak 1Peak 2Mountain Monitoring SystemRegion [0,5]: 2 peaks detectedUpdate: Elevation 3Query: Count peaksSegment Tree Benefits:โ€ข O(log n) peak countingโ€ข O(log n) elevation updatesโ€ข Efficient for large ranges
Understanding the Visualization
1
Identify Peaks
Mark mountains higher than both neighbors
2
Build Monitoring Tree
Create hierarchical system for fast region queries
3
Process Requests
Answer peak count queries and update elevations efficiently
Key Takeaway
๐ŸŽฏ Key Insight: Use a Segment Tree to transform this into a dynamic range sum problem where peak positions are maintained efficiently with logarithmic complexity for both queries and updates.
Asked in
Google 32 Amazon 28 Meta 19 Microsoft 15 Apple 12
52.5K Views
Medium-High Frequency
~35 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen