Advertisement
Kali_prasad

break-sort

Dec 28th, 2024
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.71 KB | None | 0 0
  1.  
  2. import java.util.*;
  3.  
  4. /*
  5. https://www.desiqna.in/16117/paypal-oa-october-2023-break-sort
  6.  */
  7.  
  8.  /*
  9.  
  10. //inputs for array
  11. 3 25 8
  12.  
  13. expected output -
  14. The result is 2
  15.  
  16.  
  17.  */
  18. @SuppressWarnings("unused")
  19. public class Zztemp1 {
  20.  
  21.     static Scanner sc = new Scanner(System.in);
  22.  
  23.     private static int[] getArray() {
  24.         String[] sArr = sc.nextLine().split(" ");
  25.         int[] arr = Arrays.stream(sArr).mapToInt(Integer::parseInt).toArray();
  26.         return arr;
  27.     }
  28.  
  29.     private static char[] getCharArray() {
  30.         String[] sArr = sc.nextLine().split(" ");
  31.         char[] cArr = new char[sArr.length];
  32.         for (int i = 0; i < sArr.length; i++) {
  33.             cArr[i] = sArr[i].charAt(0); // Take the first character of each string
  34.         }
  35.         return cArr;
  36.     }
  37.  
  38.     public static void main(String args[]) {
  39.         // get the inputs from terminal
  40.  
  41.        
  42.         int[] A = getArray();
  43.         int arrLen = A.length;
  44.         int rightBigNum = A[arrLen-1];//you cannot do anything with the last digit , you have to follow it
  45.         int breaks = 0;
  46.         for (int i = arrLen-2; i >=0; i-=1) {//traversing from right
  47.             int curr = A[i];
  48.             if (rightBigNum>=curr){//no need to break this abides sorted policy lets go to next taking this as rightBigNum
  49.                 rightBigNum = curr;
  50.             }else{
  51.                 int parts = (int)(Math.ceil(curr/rightBigNum));
  52.                 breaks +=(parts-1);//num of curr breaks is parts-1
  53.                 rightBigNum = curr/parts;//this is the max bound for next num which was evenly distributed
  54.             }
  55.  
  56.         }
  57.  
  58.         // //prepare the answer
  59.         System.out.println("The result is " +breaks);
  60.  
  61.     }
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement