Comparing values of two arrays at each index and c

2020-05-09 19:02发布

问题:

Comparing values of two arrays and count how many values in array2 is less than array1 i used nested for loop but it gives O(n^2) time complexity which is not efficient. Below is my code :-

#include<bits/stdc++.h>
#define fo(i, num) for (long long int i = 1; i <= num; i++)
#define foo(i,arr2) for(long long int i = 0; i < arr2.size(); i++)
#define fooo(j,arr1)for(long long int j = 0; j < arr1.size(); j++)

// now take input by yourself

 fo(i,num) 
//take input of array 1

fo(i,num) 
//take input of array 2

//then sort both arrays
for(long long int i = 0; i < arr2.size(); i++)
   for(long long int j = 0; j < arr1.size(); j++){
       if(arr2[i] < arr1[j]){     
 arr1.erase(arr1.begin()+j);
    count++;
    break;
    }}}printf("%lli",count);

By this code my code runs successfully but i get TLE in some of the inputs.

**Can anyone please help me out by make efficient code of O(n) complexity ?** `