Advertisement
coasterka

#4LongestIncreasingSequence

Jun 3rd, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.25 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class LongestIncreasingSequence {
  4.  
  5.     public static void main(String[] args) {
  6.        
  7.         Scanner scan = new Scanner(System.in);
  8.         String[] input = (scan.nextLine()).split(" ");
  9.         int[] numbers = new int[input.length];
  10.         int counter = 1;
  11.         int sequenceLength = 1;
  12.         int endPosition = 0;
  13.        
  14.         for (int i = 0; i < numbers.length; i++) {
  15.             numbers[i] = Integer.parseInt(input[i]);
  16.         }
  17.        
  18.         System.out.print(numbers[0] + " ");
  19.        
  20.         for (int i = 1; i < numbers.length; i++) {
  21.                
  22.             if(numbers[i] > numbers[i - 1]) {
  23.                 counter++;
  24.                 System.out.print(numbers[i] + " ");                        
  25.             }
  26.             else {
  27.                 counter = 1;
  28.                 System.out.println();
  29.                 System.out.print(numbers[i] + " ");
  30.             }
  31.             if(counter > sequenceLength) {
  32.                 sequenceLength = counter;
  33.                 endPosition = i;
  34.             }
  35.         }      
  36.         int startPosition = endPosition - sequenceLength + 1;
  37.         System.out.println();
  38.        
  39.         System.out.print("Longest: ");
  40.         for (int i = startPosition; i <= endPosition; i++) {
  41.             System.out.print(numbers[i] + " ");
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement