AlexKondov

Java Longest Increasing Sequence

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