Find Pair Given Difference | Practice | GeeksforGeeks

Given an array Arr[] of size L and a number N, you need to write a program to find if there exists a pair of elements in the array whose difference is N.
Example 1:
Input:
L = 6, N = 78
arr[] = {5, 20, 3, 2, 5, 80}
Output: 1
Explanation: (2, 80) have difference of 78.
Example 2:
Input:
L = 5, N = 45
arr[] = {90, 70, 20, 80, 50}
Output: -1
Explanation: There is no pair with difference of 45.
Your Task:
You need not take input or print anything. Your task is to complete the function findPair() which takes array arr, size of the array L and N as input parameters and returns True if required pair exists, else return False. 
Expected Time Complexity: O(L* Log(L)).
Expected Auxiliary Space: O(1).
Constraints:
1<=L<=104 
1<=Arr[i]<=105 
0<=N<=105
+2
itsmemritu6 days ago
bool findPair(int arr[], int size, int n){   //code   set<int>s;   for(int i=0 ;i<size ;i++){           if(s.find(n+arr[i])!=s.end() or s.find(arr[i]-n)!=s.end() ){           return true;           }        s.insert(arr[i]);   }   return false;   }
+3
yuvrajranabtcse201 week ago
c++ code
 
bool findPair(int arr[], int size, int n){   sort(arr,arr+size);   for(int i=0;i<size;i++){       int find=arr[i]+n;       int start=i+1;       int end=size-1;       int mid=start+(end-start)/2;       while(start<=end){           if(arr[mid]==find)return true;           else if(arr[mid]>find) end=mid-1;           else start=mid+1;           mid=start+(end-start)/2;       }   }   return false;   }
0
amarrajsmart1971 week ago
   map<int,int> mp;      for(int i=0;i<size;i++)   {       mp[arr[i]]++;   }   if(n==0)   {       for(auto it:mp){       if(it.second>1)       return true;       }       return false;   }   for(int i=0;i<size;i++)   {       if(mp.find(arr[i]+n)!=mp.end())       {           if(mp.find(arr[i]+n)!=mp.end())           return true;       }   }      return false;}
+1
vaibhaviitj2 weeks ago
Hi GFG TeamYou are missing this test case:5 0 1 2 2 6 5 
+1
harshsinha18082 weeks ago
Java Code using binary search

 if(n==0){
            return false;
        }
        int dif = 0;
		Arrays.sort(arr);
		for(int i = 0 ; i< size ; i++) {
			int a = arr[i];
			dif = Math.abs(n+a);
			int start =0;
			int end = size-1;
			int mid = start+(end-start)/2;
			while(start<=end) {
				if(arr[mid]==dif) {
					return true;
				}else if(arr[mid]>dif) {
					end=mid-1;
				}else {
					start=mid+1;
				}
				mid=start+(end-start)/2;
			}
		}
		return false;
0
superdude
This comment was deleted.
+2
thevaibhavkute4 weeks ago
bool findPair(int arr[], int size, int n){
    sort(arr,arr+size);
    
    int low=0;
    int high=1;
    
    while(low<size && high<size)
    {
        if(abs(arr[high]-arr[low])==n && low!=high)
            return true;
        else if((arr[high]-arr[low])<n)
            high++;
        else 
            low++;
    }
    return false;
}
0
vibhanshukaushal101 month ago
C++ SOL :
bool findPair(int arr[], int size, int n){   //code      vector<int> v ;    unordered_map<int,int> mp ;    int i  ;    for(i=0 ; i<size ; i++)   {       v.push_back(abs(arr[i]-n)) ;    }   for(i=0 ; i<size ; i++)   {       mp[arr[i]]++ ;    }   for(i=0 ; i<v.size() ; i++)   {       if(mp[v[i]]==1 && n!=0)       {           return true ;        }       else if(n==0 && mp[v[i]]==2)       {           return true ;        }   }   return false;}
-1
shivommahar1 month ago
easy c++
bool findPair(int arr[], int size, int n){   sort(arr, arr+size);   int l=0;   int h=1;   while(l<size && h<size)   {       if(arr[h]-arr[l]==n && h>l)           return true;       else       {           if(arr[h]-arr[l]>n)       l++;       else       h++;}   }   return false;   }
0
abhinavsingh471 month ago
   public boolean findPair(int arr[], int size, int n)   {       //code here.       // first sort the array to perform binary search       Arrays.sort(arr);       for(int i=0;i<size;i++){           // now we are finding the n-arr[i] number using binary search           int complementIndex = Arrays.binarySearch(arr,n+arr[i]);           // making sure that we don't find the same number because for n =0 this turns true           if(complementIndex>0 && complementIndex!=i) return true;       }              return false;   }
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.