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 INPUT Integer Array nums: 4 i=0 6 i=1 7 i=2 7 i=3 nums = [4, 6, 7, 7] Constraints: - At least 2 elements - Non-decreasing order - No duplicates Subsequence: keep order, delete some elements ALGORITHM STEPS 1 Backtracking Setup Track current path, use Set for deduplication at level 2 DFS Exploration For each index, try adding if value >= last in path 3 Skip Duplicates Use Set at each level to skip same values 4 Collect Results Add path to result when length >= 2 Decision Tree (partial): [4] [4,6] [4,7] [4,6,7] FINAL RESULT 8 Valid Subsequences: [4, 6] [4, 7] [6, 7] [7, 7] [4, 6, 7] [4, 7, 7] [6, 7, 7] [4, 6, 7, 7] Output Array: [[4,6], [4,6,7], [4,6,7,7], [4,7], [4,7,7], [6,7], [6,7,7], [7,7]] Complexity: Time: O(2^n * n) Space: O(n) for recursion OK - 8 results Key Insight: Use backtracking with a Set at each recursion level to avoid duplicate subsequences. The Set tracks which values have already been used at the current decision point. Only add elements that maintain non-decreasing order (current value >= last element in path). This ensures all valid subsequences without duplicates. TutorialsPoint - Non-decreasing Subsequences | Optimal Backtracking Solution
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