Find a Pair with a Given Difference | Love Babbar DSA Sheet | GFG | FAANG🔥 | Placement
Vložit
- čas přidán 18. 12. 2020
- #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'.
Array question Playlist = • Love Babbar DSA 450 Qu...
String question Playlist = • Love Babbar DSA 450 Qu...
Searching and Sorting question Playlist = • Love Babbar DSA 450 Qu...
Binary Tree question Playlist = • Love Babbar DSA 450 Qu...
Dynamic Programming question Playlist = • Love Babbar DSA 450 Qu...
Roadmap for Dynamic Programming = • Complete Roadmap for D...
Great Strategy to solve DSA = • Great Strategy to solv...
My Journey to 5 star at codechef = • My Journey to 5 Star a...
Love Babbar DSA Sheet : drive.google.com/file/d/1FMdN...
Follow us on Instagram:
Shailesh Yogendra : / shaileshyogendra
Yogesh Yogendra : / i_am_yogesh_here
Follow us on Linkedin:
Shailesh Yogendra : / shailesh-yogendra-8b13...
Yogesh Yogendra : / yogesh-yogendra-26bbb518a
Hope you like it. Comment if you have any doubt
LIKE | SHARE | SUBSCRIBE
Example Array - [1,3,2] and x=0; then as per your logic (1+0) that is 1 exists in the map .
Handling all the test cases😀😀
//we can use hashing
HashMap 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;
You haven't considered corner case for x=0
Bhot shi kaam kr rhe ho bhiyaa appp🔥🔥🔥
binary search method is not getting submitted on gfg
dude ur approach is good but we in constrain there is mentioned that we don't need to use extra space
edge case when all elements of array are same is not handled by method 2
int Solution::solve(vector &A, int B) {
unordered_map mp;
for(auto i : A) mp[i] = 1;
for(auto i : A){
mp[i] = 0;
if(mp[B+i] > 0) return 1;
mp[i] = 1;
}
return 0;
}
You are solving other question, the question given in love babbar sheet is present on coding ninjas
map me insert karne ki time complexity logn hoti h shayad toh , second case me nlogn hogi time complexity , im not sure though
unordered map has O(1) complexity and Map has O(LogN) complexity...and I am using unordered map
@@CodeLibrary yeah you are right
Uper bound kya hota hai.expliain clearly last method
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++
You can create your own upper bound fn in java right ?
bhaiya apan -a kyu kar rahe hai upperbound function ke baad?
3rd method 7:25
Not working for nlogn
Bro give the source code in description
bhai + q ke rahe ho map[arr[i]]-x q nhi?
same question
tqqqqqqqqqqqqqqqqqqq
this code is giving wrong answer you make fool in every video
It is pronounced as sorted not shorted
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