Wiggle Subsequence - Problem

A wiggle sequence is a sequence where the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative.

A sequence with one element and a sequence with two non-equal elements are trivially wiggle sequences.

For example, [1, 7, 4, 9, 2, 5] is a wiggle sequence because the differences (6, -3, 5, -7, 3) alternate between positive and negative. In contrast, [1, 4, 7, 2, 5] and [1, 7, 4, 5, 5] are not wiggle sequences. The first is not because its first two differences are positive, and the second is not because its last difference is zero.

A subsequence is obtained by deleting some elements (possibly zero) from the original sequence, leaving the remaining elements in their original order.

Given an integer array nums, return the length of the longest wiggle subsequence of nums.

Input & Output

Example 1 — Standard Wiggle Sequence
$ Input: nums = [1,7,4,9,2,5]
Output: 6
💡 Note: The entire array forms a wiggle sequence with differences [+6,-3,+5,-7,+3] alternating between positive and negative. All 6 elements can be included.
Example 2 — Non-Wiggle Array
$ Input: nums = [1,4,7,2,5]
Output: 4
💡 Note: The differences are [+3,+3,-5,+3]. The first two differences are both positive, so we can pick subsequence [1,7,2,5] with differences [+6,-5,+3] for length 4.
Example 3 — Equal Elements
$ Input: nums = [1,7,4,5,5]
Output: 4
💡 Note: The last difference is zero (5-5=0), so we exclude one of the final 5s. Optimal subsequence is [1,7,4,5] with length 4.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 0 ≤ nums[i] ≤ 1000

Visualization

Tap to expand
Wiggle Subsequence INPUT nums array: 1 7 4 9 2 5 Differences between elements: +6 -3 +5 -7 +3 Pattern: up-down-up-down-up 1 7 4 9 2 5 ALGORITHM STEPS 1 Initialize Counters up = 1, down = 1 Track peaks and valleys 2 Iterate Array Compare adjacent elements Check if going up or down 3 Update Counters If nums[i] > nums[i-1]: up = down + 1 If nums[i] < nums[i-1]: down = up + 1 4 Return Maximum Result = max(up, down) Trace Example: i=1: 7>1 (up) up=2 i=2: 4<7 (dn) down=3 i=3: 9>4 (up) up=4 i=4: 2<9 (dn) down=5 i=5: 5>2 (up) up=6 max(6,5) = 6 FINAL RESULT Longest Wiggle Subsequence: 1 7 4 9 2 5 All 6 elements form a wiggle! Output: 6 [OK] Complete Wiggle Sequence UP DN UP DN UP Key Insight: The greedy approach works because we only need to track the length of the longest wiggle sequence ending with an upward or downward trend. When we see an upward move, the new "up" length is previous "down" + 1, and vice versa. Time Complexity: O(n), Space Complexity: O(1). TutorialsPoint - Wiggle Subsequence | Greedy - Track Trend Changes
Asked in
Google 15 Microsoft 12 Amazon 8 Facebook 6
185.0K Views
Medium Frequency
~25 min Avg. Time
3.4K 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