Pair with given sum in a sorted array | Practice | GeeksforGeeks

You are given an array Arr of size N. You need to find all pairs in the array that sum to a number K. If no such pair exists then output will be -1. The elements of the array are distinct and are in sorted order.
Note: (a,b) and (b,a) are considered same. Also, an element cannot pair with itself, i.e., (a,a) is invalid.
Example 1:
â€‹Input:
n = 7
arr[] = {1, 2, 3, 4, 5, 6, 7}
K = 8
Output:
3
Explanation:
We find 3 such pairs that
sum to 8 (1,7) (2,6) (3,5)


â€‹Example 2:
Input:
n = 7
arr[] = {1, 2, 3, 4, 5, 6, 7}
K = 98 
Output:
-1 
 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function Countpair() that takes an array (arr), sizeOfArray (n), an integer K and return the count of the pairs which add up to the sum K. The driver code takes care of the printing.

Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
 
Constraints:
0 <= Ai <=107
2 <= N <= 107
0 <= K <= 107
0
krishnendughosh6 hours ago
Java Solution : 0.25/15.42
tc : O(n), one iteration
sc : O(1)
class Solution{
    int Countpair(int arr[], int n, int sum){
        int i = 0, j = n-1, k = 0, pair = 0;
        while(i<j){
            k = arr[i] + arr[j];
            if(k==sum){
                pair+=1;
                i++; j--;
            }else if(k<sum) i++;
            else j--;
        }
        
        if(pair==0) return -1;
        else return pair;
    }
}
0
yashaggarwal55552 days ago
class Solution{ public:   int Countpair(int arr[], int n, int sum){              // Complete the function    unordered_map<int,int> m;    int pair=0;    for(int i=0;i<n;i++)    {        int b=sum-arr[i];        if(m[b])        {            pair++;        }        ++m[arr[i]];    }    if(pair==0)       return -1;       else       return pair;   }};
0
sharib_saifi1 week ago
//Simple java Solution
class Solution{       int Countpair(int a[], int n, int sum)   {       // Complete the function       int temp=0;       for(int i=0;i<n;i++){           for(int j=i+1;j<n;j++){               if(a[i] + a[j] == sum){                   temp++;               }           }       }       if(temp == 0){           return -1;       }       else{           return temp;       }   }    }
0
tuwodabhinav071 week ago
int count=0;       for(int i=0;i<n;i++)       {           int low=i+1;           int high=n-1;                      while(low<=high)           {               int mid=low+(high-low)/2;               if(arr[i]+arr[mid]==sum)               {                   count++;                   break;               }               else if(arr[i]+arr[mid]<sum)               {                   low=mid+1;               }               else if(arr[i]+arr[mid]>sum)               {                   high=mid-1;               }           }       }       if(count==0)       return -1;       else       return count;   
0
imkprakash2 weeks ago
C++ solution :
Time taken : 0.01/ 2.62
class Solution{
  public:
    int Countpair(int arr[], int n, int sum){
        
        // Complete the function
        int count=0;
        int i=0, j=n-1;
        while(i<j){
            if(arr[i]+arr[j] == sum){
                count++;
                i++;
                j--;
            }
            else if(arr[i]+arr[j]<sum){
                i++;
            }
            else{
                j--;
            }
        }
        if(count) return count;
        return -1;
    }
};
0
kmani33072 weeks ago
int c=0;       // Complete the function       int i=0,j=1;       while(i<n){           if(j<n && arr[i]+arr[j]<sum){               j++;           }           else if(arr[i]+arr[j]==sum){               c+=1;               i++;               j=i+1;           }           else{               i++;               j=i+1;           }       }       return c>0?c:-1;
0
2020bec0654 weeks ago
    // Complete the function   sort(arr,arr+n);   int l=0;   int r=n-1;   int ans=-1;   while(l<r)       {                      if(arr[l]+arr[r]==sum)           {               ans++;           }           if(arr[l]+arr[r]>sum)           {               r--;           }           else           {               l++;           }       }   if(ans==-1){       return ans;   }          return ans+1;   }
0
patildhiren441 month ago
JAVA - 
int Countpair(int[] arr, int n, int tar)
    {
        int i = 0;
        int j = arr.length - 1;
        int cou = 0;
        while (i < j) {
            int sum = arr[i] + arr[j];

            if (sum == tar) {
                i++;
                j--;
                cou++;
            } else if (sum > tar) {
                j--;
            } else {
                i++;
            }
        }
        if (cou == 0)
            return -1;
        return cou;
    }
0
vishwakarma123411 month ago
int Countpair(int arr[], int n, int k){
        
        // Complete the function
        map<int ,int>mc;
        int count=0;
        for(int i=0;i<n;i++)
        {
            int b=k-arr[i];//b=k-arr[i]
            if(mc[b]);//freq of b
                count+=mc[b];//adding freq to count
            mc[arr[i]]++;//incrementing occurences(hashing)
        }
        if(count)
            return count;
        else 
            return -1;
    }
0
ssrkb1 month ago
       TIME COMPLEXITY - 0.02/2.62
       int count=0;       int l=0,r=n-1;       while(l<r){           if(a[l]+a[r] == sum){               count++;               l++;               r--;           }           else{               if(a[l]+a[r]<sum) l++;               else r--;           }       }       return count==0?-1:count;
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.