Guest User

Untitled

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