Tuesday, July 8, 2014

Find the Size of longest subsequent array (Subsequent array must be in increasing order)

This Java Code will find the Size of longest subsequent array (Subsequent arraywill be in increasing order)

Example:-

     Input array : { 10, 22, 9, 33, 21, 50, 41, 60, 80 }
     the largest subsequent array will be - {10, 22, 33, 50, 60, 80}
 So the Output will be - 6 

CODE:-

import java.util.Scanner;

public class Test {   

    public static int longestSeq(int[] input1) {
        int longest = 0;
        int[] longestArray = new int[input1.length];

        for (int i = 0; i < input1.length; i++) {

            longestArray[i] = 1;
            int tempCount = i ;
            for (int j = tempCount+1; j < input1.length; j++ ) {               

                if (input1[tempCount] < input1[j]) {

                    longestArray[i]++;
                    tempCount = j;
                }
            }
        }

        longest = longestArray[0];
        for (int i = 1; i<longestArray.length; i++) {

            if (longest < longestArray[i])
                longest = longestArray[i];
        }
        return longest;
    }

    public static void main(String[] args) {

        Scanner input = new Scanner (System.in);
        System.out.println ("Enter Size of an array: ");
        int n = input.nextInt();


        int[] valueArray = new int[n] ;   

        System.out.println ("Enter The array elements: ");

        for (int i = 0; i< n; i++) {

            valueArray[i] = input.nextInt();
        }   

        System.out.println("The Longest Subsequent Array size is: " + longestSeq(valueArray));

    }

}
 

No comments:

Post a Comment