Key Pair

PHOTO EMBED

Thu Jun 09 2022 06:57:22 GMT+0000 (Coordinated Universal Time)

Saved by @shivam_2009 #c++

class Solution{
public:	
	// Function to check if array has 2 elements
	// whose sum is equal to the given value
	bool hasArrayTwoCandidates(int arr[], int n, int x) {
	    sort(arr,arr+n);
	    int i=0,j=n-1;
	    while(i<j)
	    {
	      if(arr[i]+arr[j]==x)
	      return 1;
	      if(arr[i]+arr[j]>x)
	      j--;
	      if(arr[i]+arr[j]<x)
	      i++;
	    }
	    return 0;
	    
	    // code here
	}
};
content_copyCOPY

https://www.geeksforgeeks.org/given-an-array-a-and-a-number-x-check-for-pair-in-a-with-sum-as-x/