forked from ppsirker/dsalgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncreasingArraySubsequence.java
More file actions
55 lines (49 loc) · 1.18 KB
/
IncreasingArraySubsequence.java
File metadata and controls
55 lines (49 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
For problem and solution description please visit the link below
http://www.dsalgo.com/2013/03/back-to-content-increasing-array.html
*/
package com.dsalgo;
import java.util.ArrayList;
public class IncreasingArraySubsequence
{
/**
* create subsequence of a given array where every element in the
* subsequence is greater than its previous element
*
* @param args
*/
public static void main(String[] args)
{
int[] input =
{ 2, 5, 6, 1, 3 };
int length = input.length;
ArrayList<ArrayList<Integer>> table = new ArrayList<ArrayList<Integer>>();
for (int i = 0; i < length; ++i)
{
ArrayList<ArrayList<Integer>> tempTable = new ArrayList<ArrayList<Integer>>();
for (ArrayList<Integer> j : table)
{
if (j.get(j.size() - 1) <= input[i])
{
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.addAll(j);
temp.add(input[i]);
tempTable.add(temp);
}
}
table.addAll(tempTable);
ArrayList<Integer> temp = new ArrayList<Integer>();
temp.add(input[i]);
table.add(temp);
}
// output
for (ArrayList<Integer> i : table)
{
for (Integer j : i)
{
System.out.print(j + ", ");
}
System.out.println();
}
}
}