Non-decreasing Subsequences LeetCode Solution

Here, We see Non-decreasing Subsequences LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

List of all LeetCode Solution

Non-decreasing Subsequences LeetCode Solution

Non-decreasing Subsequences LeetCode Solution

Problem Statement

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]]

Non-decreasing Subsequences Leetcode Solution C++

class Solution {
public:
    vector<vector<int>> findSubsequences(vector<int>& nums) {
        vector<int>temp;
        set<vector<int>>ans;
        func(0,nums,temp,ans);
        return vector(ans.begin(),ans.end());
    }
    void func(int idx,vector<int>&nums,vector<int>&a,set<vector<int>>&ans){
        int n=nums.size();
        if(idx>=n){
            if(a.size()>=2){
                ans.insert(a);
            }
            return ;
        }
        if(!a.size()||nums[idx]>=a.back()){
            a.push_back(nums[idx]);
            func(idx+1,nums,a,ans);
            a.pop_back();
        }
        func(idx+1,nums,a,ans); 
    }
};Code language: C++ (cpp)

Non-decreasing Subsequences Solution Java

class Solution {
    public List<List<Integer>> findSubsequences(int[] nums) {
        Set<List<Integer>> ans = new HashSet<>();
        solve(nums, 0, new ArrayList<>(), ans);
        return new ArrayList<>(ans);
    }
    public void solve(int[] nums, int index, List<Integer> output, Set<List<Integer>> ans) {
        if (index >= nums.length) {
            if (output.size() > 1) {
                ans.add(new ArrayList<>(output));
            }
            return;
        }
        if (output.isEmpty() || nums[index] >= output.get(output.size() - 1)) {
            output.add(nums[index]);
            solve(nums, index+1, output, ans);
            output.remove(output.size() - 1);
        }
        solve(nums, index+1, output, ans);
    }
}Code language: Java (java)

Non-decreasing Subsequences Solution JavaScript

var findSubsequences = function(nums) {
    let res = []
    let map = {}
    let iterate = (index,temp) =>{
        if(map[temp]) return;
        if(temp.length>=2){
            res.push(temp)
        }
        for(let i =index;i<nums.length;i++){
            if(temp[temp.length-1]>nums[i]) continue;
            map[temp] = true;
            iterate(i+1,[...temp,nums[i]])
        }
    }
    iterate(0,[])
    return res;
};Code language: JavaScript (javascript)

Non-decreasing Subsequences Solution Python

class Solution(object):
    def findSubsequences(self, nums):
        ans = set()
        self.solve(nums, 0, [], ans)
        return [list(x) for x in ans]
    def solve(self, nums, index, output, ans):
        if index >= len(nums):
            if len(output) > 1:
                ans.add(tuple(output))
            return
        if not output or nums[index] >= output[-1]:
            output.append(nums[index])
            self.solve(nums, index+1, output, ans)
            output.pop()
        self.solve(nums, index+1, output, ans)Code language: Python (python)
Scroll to Top