Peaks in Array - Problem

A peak in an array arr is an element that is greater than its previous and next element in arr.

You are given an integer array nums and a 2D integer array queries. You have to process queries of two types:

  • queries[i] = [1, li, ri] - determine the count of peak elements in the subarray nums[li..ri]
  • queries[i] = [2, indexi, vali] - change nums[indexi] to vali

Return an array answer containing the results of the queries of the first type in order.

Note: The first and the last element of an array or a subarray cannot be a peak.

Input & Output

Example 1 — Basic Peak Counting
$ Input: nums = [2,8,6,1,5], queries = [[2,3,4],[1,0,4]]
Output: [1]
💡 Note: After updating nums[3] = 4, array becomes [2,8,6,4,5]. Query [1,0,4] counts peaks in range [0,4]. Only index 1 (value 8) is a peak since 8 > 2 and 8 > 6.
Example 2 — Multiple Queries
$ Input: nums = [1,3,2,4,1], queries = [[1,1,4],[2,2,5],[1,0,3]]
Output: [1,1]
💡 Note: First query [1,1,4]: checks indices 2,3 in range [1,4]. Index 2: 2 < 3 (not peak). Index 3: 4 > 2 and 4 > 1 (peak). Count=1. After update nums[2]=5: [1,3,5,4,1]. Second query [1,0,3]: checks indices 1,2 in range [0,3]. Index 1: 3 < 5 (not peak). Index 2: 5 > 3 and 5 > 4 (peak). Count=1.
Example 3 — Edge Case No Peaks
$ Input: nums = [1,2,3], queries = [[1,0,2]]
Output: [0]
💡 Note: Range [0,2] has no valid peak positions since first and last elements cannot be peaks. Only index 1 could be a peak, but 2 < 3, so no peaks found.

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
Peaks in Array - Segment Tree Approach INPUT nums array: 2 i=0 8 i=1 6 i=2 1 i=3 5 i=4 peak Queries: [2, 3, 4] - Update [1, 0, 4] - Count peaks Peak Definition: arr[i-1] < arr[i] > arr[i+1] (not first/last element) Type 1: Count | Type 2: Update ALGORITHM STEPS 1 Build Segment Tree Mark peaks: nums[1]=8 is peak 1 1 0 2 Process Query [2,3,4] Update: nums[3] = 4 Array: [2,8,6,4,5] 3 Recheck Peak Status Check indices 2,3,4 Update segment tree 4 Process Query [1,0,4] Count peaks in [0..4] Check indices 1,2,3 only After Update: [2, 8, 6, 4, 5] Peak at index 1 (8 > 2 and 8 > 6) FINAL RESULT Final Array State: 2 8 6 4 5 PEAK Peak Analysis [0..4]: Index 0: boundary - skip Index 1: 2<8>6 - PEAK Index 2,3: not peaks Index 4: boundary - skip OUTPUT [1] OK - 1 peak found Query [1,0,4] returns 1 Time: O(log n) per query Key Insight: Segment Tree stores peak count for each range. Updates only affect 3 positions (i-1, i, i+1) since peak status depends on neighbors. Range queries exclude first/last elements of subarray. Time Complexity: O(n) build + O(log n) per query. Space: O(n) for segment tree. TutorialsPoint - Peaks in Array | Segment Tree with Point Update Approach
Asked in
Google 25 Facebook 18 Amazon 15 Microsoft 12
12.5K Views
Medium Frequency
~35 min Avg. Time
450 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