Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.29 KB | None | 0 0
  1. Problem
  2. Three brothers walk into a bar. All the beverages are placed in one line at the long bar table. The size of each glass is represented in an array of integers, glasses.
  3.  
  4. The brothers will drink a round if they can find 3 consecutive glasses of the same size. The barman removes the empty glasses from the table immediately after each round.
  5.  
  6. Find the maximum number of rounds the three brothers can drink.
  7.  
  8. Example
  9.  
  10. For glasses = [1, 1, 2, 3, 3, 3, 2, 2, 1, 1], the output should be brothersInTheBar(glasses) = 3.
  11.  
  12. The brothers can start with a round of size 3, then after the glasses are cleared, a round of size 2 can be formed, followed by a round of size 1. One glass will be left at the table.
  13.  
  14. [input] array.integer glasses
  15.  
  16. The sizes of glasses in the row.
  17.  
  18. Guaranteed constraints:
  19. 1 ≤ glasses.length105,
  20. 1 ≤ glasses[i]106.
  21.  
  22. [output] integer
  23. The maximum number of rounds the brothers can drink.
  24.  
  25.  
  26. Grading
  27. The example case passes - 5%
  28.  
  29. Happy path covered - 5%
  30.  
  31. Corner cases covered - 20%
  32.  
  33. Error handling - 10%
  34.  
  35. Optimization - 40%
  36.  
  37. Code formatting - 10%
  38.  
  39. Naming conventions - 10%
  40. ************************************************
  41.  
  42.  
  43. import java.io.BufferedReader;
  44. import java.io.IOException;
  45. import java.io.InputStreamReader;
  46. import java.util.Arrays;
  47.  
  48. public class Main {
  49.     private final static int consecutiveGlassesNeeded = 3;
  50.  
  51.     public static void main(String[] args) throws IOException {
  52.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  53.  
  54.         System.out.println("Please insert numbers with single space between them:");
  55.         int[] glasses = Arrays.stream(reader.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
  56.  
  57.         int rounds = 0;
  58.         int countGlassesOfSameSize = 1;
  59.  
  60.  
  61.         while (glasses.length >= 3){
  62.             for (int glassPos = 0; glassPos < glasses.length - 1; glassPos++) {
  63.                 if (glasses[glassPos] == glasses[glassPos + 1]){
  64.                     countGlassesOfSameSize++;
  65.  
  66.                     if (countGlassesOfSameSize == consecutiveGlassesNeeded){
  67.                         rounds++;
  68.  
  69.                         //removing process
  70.                         glasses = removeTheElements(glasses, glassPos - 1);
  71.  
  72.                         //starting over from the beginning of the array
  73.                         break;
  74.                     }
  75.                 }else {
  76.                     countGlassesOfSameSize = 1;
  77.                 }
  78.             }
  79.             //end the program when there aren't any more consecutive glasses
  80.             if (countGlassesOfSameSize < consecutiveGlassesNeeded){
  81.                 break;
  82.             }
  83.             //restart the counter
  84.             countGlassesOfSameSize = 1;
  85.         }
  86.  
  87.         System.out.println(String.format("Tonight the brothers can drink in %d rounds only", rounds));
  88.     }
  89.  
  90.     private static int[] removeTheElements(int[] arr, int index){
  91.         int[] anotherArray = new int[arr.length - consecutiveGlassesNeeded];
  92.  
  93.         for (int arrPos = 0, newArrPos = 0; arrPos < arr.length; arrPos++) {
  94.             if (arrPos == index){
  95.                 arrPos += 2;
  96.                 continue;
  97.             }
  98.  
  99.             anotherArray[newArrPos++] = arr[arrPos];
  100.         }
  101.         return anotherArray;
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement