mardi 21 avril 2020

Comparing values of two arrays at each index and count if condition is true? [duplicate]

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>

 //take input by yourself

for (long long int i = 1; i <= num; i++)
 //take input of array 1

for (long long int i = 1; i <= num; i++)
 //take input of array 2

 //then sort both arrays using std::sort();
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

Aucun commentaire:

Enregistrer un commentaire