Leetcode•May 14, 2026
Non-decreasing Subsequences
Hazrat Ali
Leetcode
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.
Example 1:
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]]
Example 2:
Input: nums = [4,4,3,2,1] Output: [[4,4]]
Solution
var findSubsequences = function(nums) {
let result = [];
function backtrack(start, path) {
if (path.length >= 2) {
result.push([...path]);
}
let used = new Set();
for (let i = start; i < nums.length; i++) {
if (used.has(nums[i])) {
continue;
}
if (
path.length === 0 ||
nums[i] >= path[path.length - 1]
) {
used.add(nums[i]);
path.push(nums[i]);
backtrack(i + 1, path);
path.pop();
}
}
}
backtrack(0, []);
return result;
};