Thursday, 9 January 2014

Find length of sub sequence array in increasing order

public class MyCode {

public static void main(String[] args) {

System.out.println("L :"
+ longestSeq(new int[] { 10, 22, 9, 33, 21, 50, 41, 60, 80 }));
System.out.println("L :"
+ longestSeq(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 7, 6, 5, 4, 5,
6, 7, 8, 9, 10, 11 }));
System.out.println("**************************");
System.out.println("L :"
+ longestSeq(new int[] { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5,
13, 3, 11, 7, 15 }));
}

public static int longestSeq(int[] input1) {
int[] L = new int[input1.length];
L[0] = 1;
for (int i = 1; i < L.length; i++) {
int maxn = 0;
for (int j = 0; j < i; j++) {
if (input1[j] < input1[i] && L[j] > maxn) {
maxn = L[j];
}
}
L[i] = maxn + 1;
}
int maxi = 0;
for (int i = 0; i < L.length; i++) {
if (L[i] > maxi) {
maxi = L[i];
}
}
return (maxi);
}

}
Output:
L :6
L :11
**************************
L :6

Post a Comment