Skip to content

Commit

Permalink
Create NumberOfPairs.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitiz11101 authored Aug 25, 2024
1 parent c798ec7 commit 60b63d4
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Arrays/NumberOfPairs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Solution {
public:
int f(int val,vector<int>&brr){
int s=0,e=brr.size()-1,index=-1;
while(s<=e){
int mid=s+(e-s)/2;
if(brr[mid]<=val){
index=mid;
s=mid+1;
}
else{
e=mid-1;
}
}
return index;
}
long long countPairs(vector<int> &arr, vector<int> &brr) {
// Your Code goes here.
long long ans=0;
sort(brr.begin(),brr.end());
int one=0,two=0,threeFour=0;
int n=brr.size();
for(auto i:brr){
if(i==1){
one++;
}
else if(i==2)
two++;
else if(i==3 || i==4){
threeFour++;
}
}
for(auto i:arr){
if(i!=1){
ans+=one;
if(i==2) ans-=threeFour;
if(i==3) ans+=two;
int index=f(i,brr);
ans+=n-index-1;
}
}
return ans;
}
};

0 comments on commit 60b63d4

Please sign in to comment.