# Increasing Subsequences – Leetcode

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2

Example
Input: `[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:

1. The length of the given array will not exceed 15.
2. The range of integer in the given array is [-100,100].
3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

### Solution

So the solution here will be start with the each number in the input array and build the increasing non-duplicate subsequences. Like when we start with `4` and try to build subsequences around `4`, we will get `[4, 6], [4, 6, 7], [4, 6, 7, 7], [4, 7], [4, 7, 7]` . We will be using recursion to achieve the desired results

```package com.programtalk.learn.interview.questions;

import java.util.ArrayList;
import java.util.List;

/**
*
* @author programtalk.com
*
*/
public class IncreasingSubsequencesSolution {

public List<List<Integer>> findSubsequences(int[] input) {
List<List<Integer>> result = new ArrayList<>();
findSequence(result, new ArrayList<Integer>(), input, 0);
return result;
}

private void findSequence(List<List<Integer>> result, List<Integer> list, int[] nums, int id) {
if (list.size() >= 2) {
// add the created subsequences to the result
result.add(new ArrayList<Integer>(list));
}
List<Integer> unique = new ArrayList<Integer>();
for (int i = id; i < nums.length; i++) { if (id > 0 && nums[i] < nums[id - 1]) {
// we need increasing numbers or equal numbers
continue;
}
if (unique.contains(nums[i])) {
// skip duplicates for subsequences
continue;
}
unique.add(nums[i]);
list.add(nums[i]);
findSequence(result, list, nums, i + 1);
list.remove(list.size() - 1);
}
}

public static void main(String[] args) {
int[] input = {4,6,7,7};
List<List<Integer>> subsequences = new IncreasingSubsequencesSolution().findSubsequences(input);
System.out.println(subsequences);
}

}
```

### References

https://leetcode.com/problems/increasing-subsequences/

Like this post? Don’t forget to share it!

### One Comment

1. Ashish Ratan

The program should not compile.
You can not add int in array List type of Integer. e.g. unique.add(nums[i]); // compile time error

This site uses Akismet to reduce spam. Learn how your comment data is processed.