YavorGrancharov

Max_Sequence_of_Increasing_Elements

Jan 4th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.10 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Max_Sequence_of_Increasing_Elements {
  4.     public static void main(String[] args) {
  5.         Scanner console = new Scanner(System.in);
  6.  
  7.         String line = console.nextLine();
  8.         String[] arr = line.split("\\s+");
  9.         int[] nums = new int[arr.length];
  10.  
  11.         for (int i = 0; i < arr.length; i++) {
  12.             nums[i] = Integer.parseInt(arr[i]);
  13.         }
  14.  
  15.         int startCount = 0;
  16.         int maxCount = 0;
  17.         int currentPos = 0;
  18.         int startPos = 0;
  19.  
  20.         for (int i = 1; i < nums.length; i++) {
  21.             if (nums[i] - nums[i - 1] >= 1) {
  22.                 currentPos++;
  23.                 startPos = i - currentPos;
  24.                 if (currentPos > maxCount) {
  25.                     maxCount = currentPos;
  26.                     startCount = startPos;
  27.                 }
  28.             }
  29.             else {
  30.                 currentPos = 0;
  31.             }
  32.         }
  33.         for (int i = startCount; i <= (startCount + maxCount); i++) {
  34.             System.out.print(nums[i] + " ");
  35.         }
  36.         System.out.println();
  37.     }
  38. }
Add Comment
Please, Sign In to add comment