Advertisement
saurav_kalsoor

Mochas Query - JAVA

Dec 17th, 2021
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. // Author : Saurav Kalsoor
  2.  
  3. import java.util.*;
  4.  
  5. public class Test {
  6.  
  7.     static Scanner sc = new Scanner(System.in);
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         int n = sc.nextInt();
  12.         int q = sc.nextInt();
  13.  
  14.         int height[] = new int[n];
  15.         int query[] = new int[q];
  16.  
  17.         for (int i = 0; i < n; i++) {
  18.             height[i] = sc.nextInt();
  19.         }
  20.  
  21.         for (int i = 0; i < q; i++) {
  22.             query[i] = sc.nextInt();
  23.         }
  24.  
  25.         int[] result = mochasQuery(height, n, query, q);
  26.  
  27.         for(int i=0; i < q; i++){
  28.             System.out.print(result[i] + " ");
  29.         }
  30.  
  31.     }
  32.  
  33.     public static int[] mochasQuery(int[] height, int n, int[] query, int q) {
  34.         int result[] = new int[q];
  35.  
  36.         Arrays.sort(height);
  37.  
  38.         for(int i=0; i < q; i++){
  39.             result[i] = binarySearchX(height, n, query[i]);
  40.         }
  41.  
  42.         return result;
  43.     }
  44.  
  45.     public static int binarySearchX(int[] height, int n, int x){
  46.         int lo = 0, hi = n-1, index = n;
  47.  
  48.         while(lo <= hi){
  49.             int mid = lo + (hi - lo) / 2;
  50.            
  51.             if(height[mid] >= x){
  52.                 index = mid;
  53.                 hi = mid-1;
  54.             }else{
  55.                 lo = mid+1;
  56.             }
  57.         }
  58.  
  59.         int res = n - index;
  60.         return res;
  61.     }
  62.  
  63. }
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement