[text] 1

Viewer

  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Main {
  5.  
  6.     public static void main(String[] args) {
  7.         /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Main. */
  8.         Scanner sc=new Scanner(System.in);
  9.         int t=sc.nextInt();
  10.         while(t--!=0){
  11.             int n=sc.nextInt();
  12.             int arr[]=new int[n];
  13.             for(int i=0;i<arr.length;i++){
  14.                 arr[i]=sc.nextInt();
  15.             }
  16.             int m=sc.nextInt();
  17.             int p[]=new int[m];
  18.             for(int i=0;i<m;i++){
  19.                 p[i]=sc.nextInt();
  20.             }
  21.             HashMap<Integer,Integer> map=new HashMap<>();
  22.             for(int i=0;i<n;i++){
  23.                 map.put(arr[i],map.getOrDefault(arr[i],0)+1);
  24.             }
  25.             ArrayList<Integer> l=new ArrayList<>(map.keySet());
  26.             for(int i=0;i<p.length;i++){
  27.                 int key=p[i];
  28.                 int low=0;
  29.                 int high=l.size()-1;
  30.                 int ans=-1;
  31.                 while(low<=high){
  32.                     int mid=low+(high-low)/2;
  33.                     if(l.get(mid)>key){
  34.                         ans=mid;
  35.                         low=mid+1;
  36.                     }else{
  37.                         high=mid-1;
  38.                     }
  39.                 }
  40.                 System.out.println(ans+2);
  41.             }
  42.         }
  43.     }
  44.  
  45. }

Editor

You can edit this paste and save as new:


File Description
  • 1
  • Paste Code
  • 28 Mar-2024
  • 1.39 Kb
You can Share it: