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
+1
harendraseervi1234567892 weeks ago
Easy C++ solution
int Countpair(int arr[], int n, int k){
       unordered_map<int,int>mp;
       for(int i=0;i<n;i++){
           mp[arr[i]]++;
       }
       int ct=0;
       for(int i=0;i<n;i++){
           if(mp[k-arr[i]]) ct++;
       }
       if(ct/2==0) return -1;
       return ct/2;
   }
0
harshpandeyalfa23 weeks ago
C++
(0.03sec)
int Countpair(int arr[], int n, int sum){       unordered_set<int> s;       int count=0;       for (int i=0;i<n;i++)       {           if(s.find(sum-arr[i]) != s.end() )            count++;           s.insert(arr[i]);       }       if(count==0)        return -1;       return count;   }
+1
dangrio1 month ago
int count=0;       int left=0,right=n-1;       while(left<right){           if(arr[left]+arr[right]==sum){               count++;               left++,right--;           }           else if(arr[left]+arr[right]<sum) left++;           else right--;       }       return count==0?-1:count;
0
skjha919992 months ago
 int Countpair(int a[], int n, int sum)   {     Arrays.sort(a);     int l=0;     int r=n-1;     int count=0;     while(r>l)     {         if(a[l]+a[r]==sum)         {            count++;            l++;            r--;         }          else if(a[l]+a[r]>sum)              r--;          else              l++;     }     if(count==0)       return -1;     return count;       }
0
mayank20212 months ago
C++int Countpair(int arr[], int n, int sum){              int l=0, r=n-1, count=0;              while(l<r)        {            if(arr[l]+arr[r]==sum)             {             count++;             l++; r--;             }                        else if(arr[l]+arr[r] < sum)              l++;            else              r--;        }                return count==0?-1:count;   }
+2
badgujarsachin832 months ago
  int Countpair(int arr[], int n, int sum){
        int c=0;
        // Complete the function
        for(int i=0;i<n-1;i++){
            for(int j=i+1;j<n;j++){
                if(arr[i]+arr[j]==sum){
                    c++;
                }
            }
        }
        if(c!=0){
            return c;
        }else{
            return -1;
        }
    }
-1
rajnish243 months ago
 int Countpair(int a[], int n, int sum)   {       // Complete the function       int x=0;       for(int i=0; i<n-1; i++)       {           for(int j=i+1; j<n; j++)           {               if(a[i]+a[j]==sum)               {                   x++;               }           }       }       if(x!=0)       {           return x;       }       else       {           return -1;       }   }
0
akashkhurana283 months ago
EASY HASHSET SOLUTION
int Countpair(int a[], int n, int sum)   {       // Complete the function            int count=0;           HashSet<Integer>hs=new HashSet<Integer>();           for(int i=0;i<n;i++){               if(hs.contains(sum-a[i]))               count++;               hs.add(a[i]);           }           if(count==0)               return -1;           else               return count;
}
0
tanishkaashi5673 months ago
class Solution:
    def Countpair (self, arr, n, sum) : 
        #Complete the function 
        l = 0 
        h = n-1 
        count = 0
        while l < h:
            
            if arr[l] + arr[h] == K:
                count +=1 
                l += 1 
                h -=1 
            elif arr[l] + arr[h] < K:
                l += 1 
            elif arr[l] + arr[h] > K:
                h -= 1 
            
        if count == 0:
            return -1
        return count
0
sagrikasoni3 months ago
class Solution{
    
  
    int Countpair(int a[], int n, int sum)
    {  
        int  i =0, j= a.length-1, count=0;
        while(i<j){
            if(a[i]+a[j]>sum)
            j--;
            else if(a[i]+a[j]<sum)
            i++;
            else{
                count++;
                i++;
                j--;
            }
        }
        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.