Find a Pair with a Given Difference | Love Babbar DSA Sheet | GFG | FAANG🔥 | Placement

preview_player
Показать описание
#sorting and searching #competitiveprogramming #coding #dsa
Hey Guys in this video I have explained with code how we can solve the problem 'Find a Pair with a Given Difference'.

Follow us on Instagram:

Follow us on Linkedin:

Hope you like it. Comment if you have any doubt

LIKE | SHARE | SUBSCRIBE
Рекомендации по теме
Комментарии
Автор

Handling all the test cases😀😀

//we can use hashing
HashMap<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < size; i++){
map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
}
for(int i = 0; i < size; i++){
int curr = arr[i];
int pair = n + arr[i];
if(n > 0 && map.containsKey(pair)){
return true;
}
if(n == 0){
//check for -curr
if(map.containsKey(-1 * pair)){
return true;
}
//one more edge case
//check for same curr and count
if(map.containsKey(pair) && map.get(pair) > 1){
return true;
}
}
}
return false;

deepakajay
Автор

Example Array - [1, 3, 2] and x=0; then as per your logic (1+0) that is 1 exists in the map .

aniketshankar
Автор

You haven't considered corner case for x=0

prateeksomani
Автор

binary search method is not getting submitted on gfg

ridhamgodha
Автор

at 9:00, upper bound etc is not there in java, kindly use methods which are generic, and not standard libraries for c or c++

rohandevaki
Автор

dude ur approach is good but we in constrain there is mentioned that we don't need to use extra space

yashyadav
Автор

Uper bound kya hota hai.expliain clearly last method

SajanKumar-ecus
Автор

You are solving other question, the question given in love babbar sheet is present on coding ninjas

princemathur
Автор

edge case when all elements of array are same is not handled by method 2

tombrady
Автор

Bhot shi kaam kr rhe ho bhiyaa appp🔥🔥🔥

anujsoni
Автор

map me insert karne ki time complexity logn hoti h shayad toh, second case me nlogn hogi time complexity, im not sure though

stunnerhash
Автор

bhaiya apan -a kyu kar rahe hai upperbound function ke baad?

sanjogbhalla
Автор

Bro give the source code in description

faizanahmad-ocfw
Автор

bhai + q ke rahe ho map[arr[i]]-x q nhi?

trialaccount
Автор

this code is giving wrong answer you make fool in every video

navendraagrawal
Автор

It is pronounced as sorted not shorted

vaibhavchhabra
Автор

Failing for testcase :5 0
1 2 2 6 5
with method3 i tried on gfg.

bool findPair(int arr[], int n, int k){


bool flag=false;
sort(arr, arr+n);
if (k==0) return flag;
for(int i=0;i<n;i++){
int idx=upper_bound(arr+i, arr+n, k+arr[i])-arr-1;
if(arr[idx]-arr[i]==k){
flag=true;
break;
}
}

return flag;

}

testcase 111/112 failed

saurabhsingh