Pete1

Untitled

Apr 2nd, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. package CollectionsHomework;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. * Created by petar on 25.3.2016 г..
  7. */
  8. public class Problem4LongestIncreasingSequence {
  9. public static void main(String[] args) {
  10. Scanner console = new Scanner(System.in);
  11.  
  12. String[] inputNumbers = console.nextLine().split(" ");
  13. int sequenceLength = 1;
  14. int sequenceStartIndex = 0;
  15. int bestSequenceLength = -50;
  16.  
  17. int[] input = new int[inputNumbers.length];
  18. for (int i = 0; i < input.length; i++) {
  19. input[i] = Integer.parseInt(inputNumbers[i]);
  20.  
  21. }
  22. System.out.print(input[0]);
  23. for (int i = 1; i < input.length; i++) {
  24. if (input[i] > (input[i - 1])) {
  25. System.out.print(" " + input[i]);
  26. sequenceLength++;
  27. if (sequenceLength > bestSequenceLength) {
  28. bestSequenceLength = sequenceLength;
  29. sequenceStartIndex = i - bestSequenceLength + 1;
  30. }
  31.  
  32. } else {
  33. System.out.println();
  34. System.out.print(input[i]);
  35. sequenceLength = 1;
  36.  
  37. }
  38.  
  39. }
  40.  
  41. System.out.println();
  42. System.out.print("Longest: ");
  43. if (bestSequenceLength < 0) {
  44. System.out.print(input[0]);
  45. }
  46. for (int i = sequenceStartIndex; i < bestSequenceLength + sequenceStartIndex; i++) {
  47. System.out.print(input[i] + " ");
  48.  
  49. }
  50.  
  51.  
  52. }
  53.  
  54.  
  55. }
Add Comment
Please, Sign In to add comment