Advertisement
YavorGrancharov

Max_Sequence_of_Equal_Elements

Jan 4th, 2018
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.01 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Max_Sequence_of_Equal_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 res = 0;
  16.         int count = 1;
  17.         int maxCount = 1;
  18.  
  19.         for (int i = 0; i < nums.length - 1; i++) {
  20.             if (nums[i] == nums[i + 1]) {
  21.                 count++;
  22.                 if (count > maxCount) {
  23.                     maxCount = count;
  24.                     res = nums[i];
  25.                 }
  26.             }
  27.             else {
  28.                 count = 1;
  29.             }
  30.             if (maxCount == 1) {
  31.                 res = nums[0];
  32.             }
  33.  
  34.         }
  35.         for (int i = 0; i < maxCount; i++) {
  36.             System.out.print(res + " ");
  37.         }
  38.     }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement