Quick actions

cmd+k|ctrl+k

Navigation

Languages

Pair with given sum in sorted array

Snippet info

Language

Cpp

Visibility

public

Author

singhrishabh557

Created

2019-01-27T14:23:40Z

Updated

2019-01-27T14:39:57Z

#include <iostream>
#include<array>
using namespace std;
bool pairSum(int arr[],int d)
{
   // int n =  arr::arr.size();
     int l=0;
     int r=5;
     
     while(l!=r)
     {
         if(arr[l]+arr[r] == d)
         return true;
         
         else if(arr[l]+arr[r] < d)
         l++;
         else if(arr[l]+arr[r] > d)
         r--;
         
         
     }
   return false;
     
}


int main() {
   int arr[] = {2,5,7,11,26,50}; 
    int d = 18; 
    
    if (pairSum(arr, d)) 
        cout << "Array has two elements with sum 16"; 
    else
        cout << "Array doesn't have two elements with sum 16 ";
    return 0;
}
INFO