Advertisement
saurav_kalsoor

Partition - JAVA

Jul 19th, 2022
673
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Author : Saurav Kalsoor
  2. // Partition - JAVA
  3.  
  4. import java.util.*;
  5.  
  6. public class Test {
  7.     static Scanner sc = new Scanner(System.in);
  8.     public static void main(String[] args) {
  9.         int n = sc.nextInt();
  10.         int k = sc.nextInt();
  11.         int x = sc.nextInt();
  12.  
  13.         ArrayList<Integer> arr = new ArrayList<>();
  14.         for(int i = 0;i < n; i++){
  15.             arr.add(sc.nextInt());
  16.         }
  17.  
  18.         System.out.println(partition(n, k, x, arr));
  19.     }
  20.  
  21.     public static int partition(int n, int k, int x, ArrayList<Integer> arr){
  22.         Collections.sort(arr);
  23.         int count = 1;
  24.         PriorityQueue<Integer> pq = new PriorityQueue<>();
  25.         int i = 1;
  26.         while(i < n){
  27.             int diff = arr.get(i) - arr.get(i-1);
  28.             if(diff > k){
  29.                 count++;
  30.                 pq.offer(diff);
  31.             }
  32.             i++;
  33.         }
  34.        
  35.         while(!pq.isEmpty() && x > 0){
  36.             int diff = pq.poll();
  37.             int req = (diff - 1)/k;
  38.             if(x >= req){
  39.                 x -= req;
  40.                 count--;
  41.             }else{
  42.                 break;
  43.             }
  44.         }
  45.        
  46.         return count;
  47.     }
  48.    
  49. }
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement