Counting elements in two arrays | Practice | GeeksforGeeks

Given two unsorted arrays arr1[] and arr2[]. They may contain duplicates. For each element in arr1[] count elements less than or equal to it in array arr2[].
Example 1:
Input:
m = 6, n = 6
arr1[] = {1,2,3,4,7,9}
arr2[] = {0,1,2,1,1,4}
Output: 4 5 5 6 6 6
Explanation: Number of elements less than
or equal to 1, 2, 3, 4, 7, and 9 in the
second array are respectively 4,5,5,6,6,6

Example 2:
Input:
m = 5, n = 7
arr1[] = {4 8 7 5 1}
arr2[] = {4,48,3,0,1,1,5}
Output: 5 6 6 6 3
Your Task :
Complete the function countEleLessThanOrEqual() that takes two array arr1[], arr2[],  m, and n as input and returns an array containing the required results(the count of elements less than or equal to it in arr2 for each element in arr1 where ith output represents the count for ith element in arr1.)
Expected Time Complexity: O((m + n) * log n).
Expected Auxiliary Space: O(1).
Constraints:
1<=m,n<=10^5
1<=arr1[i],arr2[j]<=10^5
0
vikasrajpoot4791 day ago
//using binary search
class Solution{ public:   vector<int> countEleLessThanOrEqual(int arr1[], int arr2[],                                 int m, int n)   {    sort(arr2,arr2+n);    vector<int>sol;    for(int i=0;i<m;i++)    {        int k=arr1[i];        int s=0;        int e=n-1;                int ans=0;        while(s<=e)        {            int mid=s+(e-s)/2;                        if(arr2[mid]<=k)            {                ans=mid+1;                s=mid+1;            }            else{                e=mid-1;            }        }        sol.push_back(ans);    }    return sol;   }};
0
ajay7yadav956 days ago
//hy javaScript lovers ----------------→ A7
 
arr2.sort((x,y)=>x-y);       for(let i=0; i<arr1.length; i++){           let x = arr1[i];           let l=0;           let r=arr2.length-1;           let count = 0;           while(l<=r){               let mid = parseInt(l+(r-l)/2);               if(arr2[mid]<= x){                   count = mid+1;                   l = mid+1;               }               if(arr2[mid]> x){                   r = mid -1;               }           }           arr1[i] = count;       }       return arr1;
0
tanashah1 week ago
 int counforfirst(int arr[],int low,int high ,int x){
        while(low<=high){
           int mid=(low+high)/2;
           
           if(arr[mid]<=x){
               low=mid+1;
           }
           else{
               high=mid-1;
           }
          }
         return high; //nichese element hai uska index print karvare rahe hai simple
        
    }
    vector<int> countEleLessThanOrEqual(int arr1[], int arr2[], 
                                 int m, int n)
    {
    //Your code goes here
    //O(n^2) soln 16-02
        vector<int>v;
         sort(arr2,arr2+n);
         for(int i=0;i<m;i++){
             
            int count=counforfirst(arr2,0,n-1,arr1[i]);
             v.push_back(count+1);
         }
         return v;
    }
0
hr15171517hr1 week ago
//JAVA USING BS
class Solution
{
    public static ArrayList<Integer> countEleLessThanOrEqual(int arr1[], int arr2[], int m, int n)
    {
        ArrayList<Integer> al = new ArrayList<>();
        Arrays.sort(arr2);
        for (int i = 0; i < m; i++) {
            int x = ceiling(arr2, arr1[i]);
            if(x==-1){
                al.add(n);
            }
            else{
                al.add(x);
            }
        }
        return al;
    }
    static int ceiling(int arr[], int x) {
        int res = -1;
        int low = 0, high = arr.length - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (arr[mid] > x) {
                res = mid;
                high = mid - 1;
            } else if (arr[mid] <= x) {
                low = mid + 1;
            }
        }
        return res;
    }
}

0
itsmeshahid1 week ago
int bs(int arr[], int low, int high, int x){              while(low<=high){           int mid=(low+high)/2;                      if(arr[mid]<=x){               low=mid+1;           }           else{               high=mid-1;           }       }       return high;   }         vector<int> countEleLessThanOrEqual(int arr1[], int arr2[],                                 int m, int n)   {                        sort(arr2, arr2+n);            vector<int>ans;                        for(int i=0; i<m; i++){                                int index=bs(arr2, 0, n-1, arr1[i]);                ans.push_back(index+1);            }                        return ans;                  }
0
itsmeshahid
This comment was deleted.
0
skjha919992 weeks ago
ArrayList<Integer>a=new ArrayList<>();     Set<Integer>h=new LinkedHashSet<>();     for(int i=0;i<m;i++)     {       h.add(arr1[i]);     }     //System.out.print(h);     Iterator iterator = h.iterator();       while(iterator.hasNext())     {       int i1=(Integer)iterator.next();       int count=0;       for(int i=0;i<n;i++)        {         if(arr2[i]<=i1)             count++;        }        a.add(count);     }       return a;     }
0
harshilrpanchal19983 weeks ago
java code
 
ArrayList<Integer> list = new ArrayList<>();
              Arrays.sort(arr2);       int count =0;       for (int i=0 ; i < arr1.length ; i++){            count =0;           for (int j=0 ; j < arr2.length ; j++){
               if(arr1[i] >= arr2[j]){                   count++;               }
           }           list.add(count);                 }       return list;
0
tanishkaashi5671 month ago
Time limit is exceeding if we use an array to return the result. (I know they asked us to solve it in O(1) space complexity. But the driver code is taking list as an output. What to do about that? Otherwise my code is working fine. Just not able to submit it due to space complexity issue!! please fix it.
+1
guruprasadnb14021 month ago
vector<int> countEleLessThanOrEqual(int arr1[], int arr2[],                                 int m, int n)   {   //Your code goes here   vector<int>ans;   sort(arr2,arr2+n);   for(int i=0;i<m;++i){       auto count=upper_bound(arr2,arr2+n,arr1[i]);       ans.push_back(count-arr2);   }   return ans;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.