Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package CollectionsHomework;
- import java.util.Scanner;
- /**
- * Created by petar on 25.3.2016 г..
- */
- public class Problem4LongestIncreasingSequence {
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- String[] inputNumbers = console.nextLine().split(" ");
- int sequenceLength = 1;
- int sequenceStartIndex = 0;
- int bestSequenceLength = -50;
- int[] input = new int[inputNumbers.length];
- for (int i = 0; i < input.length; i++) {
- input[i] = Integer.parseInt(inputNumbers[i]);
- }
- System.out.print(input[0]);
- for (int i = 1; i < input.length; i++) {
- if (input[i] > (input[i - 1])) {
- System.out.print(" " + input[i]);
- sequenceLength++;
- if (sequenceLength > bestSequenceLength) {
- bestSequenceLength = sequenceLength;
- sequenceStartIndex = i - bestSequenceLength + 1;
- }
- } else {
- System.out.println();
- System.out.print(input[i]);
- sequenceLength = 1;
- }
- }
- System.out.println();
- System.out.print("Longest: ");
- if (bestSequenceLength < 0) {
- System.out.print(input[0]);
- }
- for (int i = sequenceStartIndex; i < bestSequenceLength + sequenceStartIndex; i++) {
- System.out.print(input[i] + " ");
- }
- }
- }
Add Comment
Please, Sign In to add comment