Advertisement
dimipan80

4. Longest Increasing Sequence

Sep 12th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. /* Write a program to find all increasing sequences inside an array of integers.
  2.  * The integers are given in a single line, separated by a space.
  3.  * Print the sequences in the order of their appearance in the input array,
  4.  * each at a single line. Separate the sequence elements by a space.
  5.  * Find also the longest increasing sequence and print it at the last line.
  6.  * If several sequences have the same longest length, print the leftmost of them. */
  7.  
  8. import java.util.ArrayList;
  9. import java.util.Scanner;
  10.  
  11. public class _04_LongestIncreasingSequence {
  12.  
  13.     public static void main(String[] args) {
  14.         // TODO Auto-generated method stub
  15.         Scanner scan = new Scanner(System.in);
  16.         System.out.println("Enter the Elements of your Array on single line, separated by a space:");
  17.         String inputLine = scan.nextLine();
  18.  
  19.         if (!inputLine.isEmpty()) {
  20.             System.out.println("The All Increasing Sequences of elements in your Array are:");
  21.             String[] numStr = inputLine.split("[\\s]+");
  22.             if (numStr.length > 1) {
  23.                 int[] numberArr = new int[numStr.length];
  24.                 for (int i = 0; i < numberArr.length; i++) {
  25.                     numberArr[i] = Integer.parseInt(numStr[i]);
  26.                 }
  27.  
  28.                 findAndPrintAllIncreasingSequencesInArrayOfIntegers(numberArr);
  29.             } else {
  30.                 System.out.println(numStr[0]);
  31.                 System.out.println("Longest: " + numStr[0]);
  32.             }
  33.         } else {
  34.             System.out.println("Error! - Input Array is Empty!!!");
  35.         }
  36.     }
  37.  
  38.     private static void findAndPrintAllIncreasingSequencesInArrayOfIntegers(
  39.             int[] numbers) {
  40.         int longestSeqLength = 1;
  41.         int startIndex = 0;
  42.         int elementsCounter = 1;
  43.         int indexElements = 0;
  44.  
  45.         StringBuilder sequence = new StringBuilder();
  46.         sequence.append(numbers[0]);
  47.         for (int i = 1; i < numbers.length; i++) {
  48.  
  49.             if (numbers[i] > numbers[i - 1]) {
  50.                 sequence.append(' ').append(numbers[i]);
  51.                 elementsCounter++;
  52.             } else {
  53.                 if (elementsCounter > longestSeqLength) {
  54.                     longestSeqLength = elementsCounter;
  55.                     startIndex = indexElements;
  56.                 }
  57.  
  58.                 System.out.println(sequence);
  59.                 indexElements = i;
  60.                 elementsCounter = 1;
  61.                 sequence = new StringBuilder();
  62.                 sequence.append(numbers[i]);
  63.             }
  64.         }
  65.  
  66.         if (sequence.length() > 0) {
  67.             if (elementsCounter > longestSeqLength) {
  68.                 longestSeqLength = elementsCounter;
  69.                 startIndex = indexElements;
  70.             }
  71.  
  72.             System.out.println(sequence);
  73.         }
  74.  
  75.         System.out.print("Longest: ");
  76.         for (int i = startIndex; i < startIndex + longestSeqLength; i++) {
  77.             System.out.print(numbers[i]);
  78.             if (i < startIndex + longestSeqLength - 1) {
  79.                 System.out.print(" ");
  80.             }
  81.         }
  82.     }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement