SvetlanPetrova

MaxSequenceOfEqualElements SoftUni

Jun 15th, 2021
594
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package JavaFund_Arrays_Exercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Collections;
  5. import java.util.Scanner;
  6.  
  7. public class LadyBuds_10 {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.        int [] array = Arrays.stream(scanner.nextLine().split(" ")).mapToInt(e -> Integer.parseInt(e)).toArray();
  12.        int maxLength = 0;
  13.        int length = 1;
  14.  
  15.        int startIndex = 0;
  16.        int bestStart = 0;
  17.  
  18.         for (int i = 1; i < array.length; i++) {
  19.             if(array[i] == array[i - 1]) {
  20.                 length++;
  21.             } else {
  22.                 length = 1;
  23.                 startIndex = i;
  24.             }
  25.  
  26.             if(length > maxLength) {
  27.                 maxLength = length;
  28.                 bestStart = startIndex;
  29.             }
  30.         }
  31.  
  32.         for (int i = bestStart; i < bestStart + maxLength; i++) {
  33.             System.out.print(array[i] + " ");
  34.         }
  35.  
  36.     }
  37. }
  38.  
RAW Paste Data