RGAlex

O7. Max Sequence Of Equal Elements

Feb 2nd, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. package ArraysExercise;
  2.  
  3. import java.util.Arrays;
  4. import java.util.Scanner;
  5.  
  6. public class O7MaxSequenceOfEqualElements {
  7.     public static void main(String[] args) {
  8.  
  9.         Scanner scanner = new Scanner(System.in);
  10.         int[] numbers = Arrays.stream(scanner.nextLine().split(" "))
  11.                 .mapToInt(Integer::parseInt).toArray();
  12.         int maxCount = 0;
  13.         int digit = 0;
  14.         int counter = 1;
  15.  
  16.         for (int i = 1; i < numbers.length; i++) {
  17.             if (numbers[i] == numbers[i - 1]) {
  18.                 counter++;
  19.  
  20.                 if (counter > maxCount) {
  21.                     maxCount = counter;
  22.                     digit = numbers[i];
  23.                 }
  24.             } else
  25.                 counter = 1;
  26.         }
  27.  
  28.         for (int i = 0; i < maxCount; i++) {
  29.             System.out.print(digit + " ");
  30.         }
  31.     }
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
Advertisement
Add Comment
Please, Sign In to add comment