Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
1,623 views
Follow up : Find the number of pairs in array whose sum>=x

Follow up : Find the number of pairs in array whose sum<=x

Expectation :

O(n) time and O(n) space ..

Followup : O(n) time and O(1) space .

Rules :

1)Explanation of algorithm in your answer is a must .

2)Writing code is optional .

3)If code is written , make sure to you use format option from the editor .
in Algorithms and Problems by Expert (108,100 points) | 1,623 views

1 Answer

0 like 0 dislike
Normal 2 Pointer approach

 

int solve(vector<int> &arr,int x){

int l=0,r=arr.size()-1;

int ans=0;

while(l<r){

if(arr[l]+arr[r]>=x){

ans+= (r-l);

r--;

}

else{

l++;

}

}

return ans;

 

}
by
0 0
Why ans+=(r-l)