Advertisement
reathh

03. Largest Increasing Sequence

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