Advertisement
CR7CR7

maxSequence

Dec 5th, 2022
525
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.Collections;
  3. import java.util.Scanner;
  4.  
  5. public class LadyBuds_10 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.        int [] array = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
  10.        int maxLength = 0;
  11.        int length = 1;
  12.  
  13.        int startIndex = 0;
  14.        int bestStart = 0;
  15.  
  16.         for (int i = 1; i < array.length; i++) {
  17.             if(array[i] == array[i - 1]) {
  18.                 length++;
  19.             } else {
  20.                 length = 1;
  21.                 startIndex = i;
  22.             }
  23.  
  24.             if(length > maxLength) {
  25.                 maxLength = length;
  26.                 bestStart = startIndex;
  27.             }
  28.         }
  29.  
  30.         for (int i = bestStart; i < bestStart + maxLength; i++) {
  31.             System.out.print(array[i] + " ");
  32.         }
  33.  
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement