Lyubohd

07. Max Sequence of Equal Elements

Jan 30th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.91 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scan = new Scanner(System.in);
  6.         String[] input = scan.nextLine().split(" ");
  7.         int[] numbers = new int[input.length];
  8.         for (int i = 0; i < numbers.length; i++) {
  9.             numbers[i] = Integer.parseInt(input[i]);
  10.         }
  11.         int length = 1;
  12.         int endIndex = -1;
  13.         int bestLength = 0;
  14.         for (int i = 0; i < numbers.length - 1; i++) {
  15.             if (numbers[i] == numbers[i + 1]) {
  16.                 length++;
  17.                 if (length > bestLength) {
  18.                     bestLength = length;
  19.                     endIndex = i + 1;
  20.                 }
  21.             } else {
  22.                 length = 1;
  23.             }
  24.         }
  25.  
  26.         for (int i = endIndex; i > endIndex - bestLength; i--) {
  27.             System.out.print(numbers[i] + " ");
  28.         }
  29.     }
  30. }
Add Comment
Please, Sign In to add comment