Given an array A[] and a number x, check for pair in A[] with sum as x | GeeksforGeeks

preview_player
Показать описание

This video is contributed by Harshit Jain.
Рекомендации по теме
Комментарии
Автор

if i just knew this channel before going to my last interview i would have got the job :(
the interviewer asked me this question and i wasn't able to solve it using the hashmap method
i am very upset to find out how much easy the solution is :(

yazanshakhshir
Автор

In Python you do not need to know the range of the hash map ie Python dictionary since they have dynamic sizes. I found Python to be a very useful language for coding interviews. Anyway thx a lot for the video!

hermesmercuriustrismegistu
Автор

Wow!! Math and hashmap are two wonderful creation of the coding universe ❤️❤️❤️❤️❤️ thanks a lot! This is amazing 🤩🤩🤩🤩🤩🤩🤩🤩🤩🤩 I get the algorithm now

タクリス
Автор

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)
{
map<int, int>mp;
for(int i=0;i<n;i++)
{
mp[arr[i]]++;
}
for(int i=0;i<n;i++)
{

{

{
return 1;
}
else if(x-arr[i]!=arr[i])
{
return 1;
}
}
}
return 0;
}
};

wecan
Автор

what if sum value is -ve then how to solve using hashmap?

akash
Автор

I like the way u r teaching sir it's geat;

divyansh
Автор

sir what about sum=2 in the following example given at 0:26 using hashmap technique

rahulkumar-uwos
Автор

Using brute force approach we can do it too using two for loops but worst time is O(n^2).

Sahil-explorer
Автор

the bool map will not work if there is repetitions in the array?

BtcTao
Автор

What about the extra space that we use for the hash array?

sounavapal
Автор

With Hashing technique what if numbers are duplicate

nshukla
Автор

The second method will never work if there is negative number in the given set of number (For C lang)

joydeepbhattacharjee
Автор

binMap[arr[i]] = 1 ---> This line in code will throw index out of bound if the array has negative numbers.

simplybish
Автор

Can someone explain third method i.e using remainders of the elements less than x....i couldnt find any video related to it

rapperstories
Автор

Why u r using hasmap for second method, hashset is better here.

mohammadmujahid
Автор

In hash method the program not working

axel
Автор

I don't think hashmap solution works right of there are dups

combofriend
Автор

i want to display those numbers in array..

ravindrasutar
Автор

I didn't understand the second method

aakashmudigonda
Автор

Damn i need to know wtf the sum is im in third im thinking about it so hard what about you give the awnser to the ppl???

marthamontes