Non-decreasing Subsequences - Problem

Given an integer array nums, return all the different possible non-decreasing subsequences of the given array with at least two elements. You may return the answer in any order.

A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements.

Note that the solution set must not contain duplicate subsequences.

Input & Output

Example 1 — Basic Case
$ Input: nums = [4,6,7,7]
Output: [[4,6],[4,6,7],[4,6,7,7],[4,7],[4,7,7],[6,7],[6,7,7],[7,7]]
💡 Note: All possible non-decreasing subsequences with at least 2 elements. Note that [4,7,7] and [6,7,7] are different subsequences even though they contain the same values because they use different indices.
Example 2 — Minimum Size
$ Input: nums = [4,4,3,2,1]
Output: [[4,4]]
💡 Note: Only [4,4] forms a valid non-decreasing subsequence with at least 2 elements. All other pairs are decreasing.
Example 3 — All Same Values
$ Input: nums = [1,1,1]
Output: [[1,1],[1,1,1]]
💡 Note: We can form [1,1] (using any two 1s) and [1,1,1] (using all three 1s). Note that duplicate subsequences are eliminated.

Constraints

  • 1 ≤ nums.length ≤ 15
  • -100 ≤ nums[i] ≤ 100

Visualization

Tap to expand
Non-decreasing Subsequences Problem4677Input: [4,6,7,7][4,6][4,6,7][4,7][6,7][6,7,7][7,7]Non-decreasing subsequences with length ≥ 2Output: 8 valid subsequences foundEach subsequence maintains order: a[i] ≤ a[i+1] ≤ ... ≤ a[j]
Understanding the Visualization
1
Input Array
Array [4,6,7,7] with repeated values
2
Find Subsequences
Extract all non-decreasing subsequences with length ≥ 2
3
Result
All valid subsequences without duplicates
Key Takeaway
🎯 Key Insight: Use backtracking to explore all possibilities while preventing duplicates with a set at each recursion level
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
125.0K Views
Medium Frequency
~15 min Avg. Time
1.9K 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