Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //first approach is brute force
- int pairs_count(int arr[],int n,int k){
- int l = 0, r = n-1,res= 0;
- while (l < r){
- if (arr[l] + arr[r] < k){ // If current left and current right have sum smaller than x,
- res += (r - l); // the all elements from l+1 to r form a pair with current l.
- l++;
- }
- else // Move to smaller value
- r--;
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement