Pairs with Positive Negative values | GeeksforGeeks | Hashing data structure | Leetcode DSA series
Vložit
- čas přidán 9. 05. 2021
- This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Pairs with Positive Negative values" from geeks for geeks which are solved by using unordered_map, vector, map in c++.
Join My Telegram channel for more Updates: telegram.me/helloworldbyprince
Given an array of distinct integers, print all the pairs having positive value and negative value of a number that exists in the array.
NOTE: If there is no such pair in the array, return empty array.
Input:
n = 6
a[] = {1, -3, 2, 3, 6, -1}
Output:
-1 1 -3 3
We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------
Pairs with Positive Negative values: practice.geeksforgeeks.org/pr...
code in This Video: github.com/Prince-1501/Hello_...
Pdf in this video: github.com/Prince-1501/Hello_...
----------------------------------------------------------------------------------------
*Follow me *
LinkedIn► / iamprince
Facebook► / helloworldofficials
Instagram► / helloworldbyprince
Twitter► / prince_king_
Telegram► telegram.me/helloworldbyprince
----------------------------------------------------------------------------------------
►Our Playlists on:-
►Competitive Programming: • How to start Competiti...
►C++ Full Course : • L-01 || Introduction a...
►Algorithms: • L-01 || Prefix Sum Arr...
►Data Structure: • Data Structures with C...
------------------------------------------------------------------------
🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
✨ Tags ✨
how to find Pairs with Positive Negative values
question asked in Goldman Sachs interview
how to crack Goldman Sachs online test
how to crack Google Interview
off-campus placement
how to learn to code for beginners
Practice Hashing data structure
hashing in data structure
Best Telegram channel for Off-campus Placement drive
hashing in a data structure in Hindi
unordered_map
vector in cpp
#hashing #geeksforgeeks #programming
you are the best........masum.
Thank you so much
Amazing
Awesome videos
Thanks
prince sir ,aap internship ke bar me bath karo
what will be the process of internships
what are skills one need to be gained to crack internships
What we need to learn.
and many
please prince bhai make a video on this
Sure
your video and explanation is great !!! but u deserve more views nd subscriber.... thank u for this video
Thanks for the support
Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
KHUD SOLVE KARLIYA BHAI CONFIDENCE INFINITTY PAR HAI LOVE FROM UTTARAKHAND
Waoooooooooooooooooooooooooooooooo 😊
Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀
14/18 DONE (9.12.22)
Solved it without extra space .Use sorting and two pointer algo
sort(a,a+n);
int i=0,j=n-1;
vectorres;
while(iabs(a[j]))
i++;
else
j--;
}
return res;
Can u explain your insert operation ..why you used res.begin() there and not only a[j] and a[i] .?????
nice aproach but we have to solve it by map to understand map more
@@avibirla9863 agr y nhi kia to order shi nhi bnega u should dry run it
Please tell here what is res.begin() will store?
I think for 3rd solution, there is no specific use of ordered map which can also be replaced with unordered map...
any way nice logic brother.. I m feeling more confident, Thank you brother
Good point
good observation
Yes! I have solved the problem using the unordered map.
was solving this....1hr passed ...1.5hr passed still not got successful submission............but after trying for 1hr +40 min.........woho!!!!!!!!!!
I made a successful submission without looking at a single hint
🙂feeling very confident
A big thanks to u bhaiya❤
class Solution{
public:
vector PosNegPair(int a[], int n) {
vectorans;
unordered_mapmp;
sort(a,a+n);
for(int i=0;i=1){
int key=abs(a[i]);
ans.push_back(-key);
ans.push_back(key);
int oppo=-a[i];
mp[oppo]--;
}
else{
mp[a[i]]++;
}
}
return ans;
}
};
bhaiyaa😍😍😍mjaa aagyaa
Bass yahi mazaa india me sabko dena hai 😂😂
Keep it up bhaiya ji..🙏🏻
Thanks ☺️
Sir ji I have tried any my 80% of approach is similar to your approach
the thing is I am not very pro in implementation but I have tried
Excellent
hey brother when u explained the sol using unordered set it will work fine as per solution as in question it is given to us the array contains all distinct integers means no repetitive element is present in array.
Yess u r udit ✅
m[data ] represent here ....in last few vedio u used either find method or count method
The approach i tried was making a map and storing values then i searched if (-key) is present or not and if present , print out -key and key.
Exactly same
Thanks AA lot bhaiya
This playlist is very helpful
Love from iitkgp
Always welcome
its already written in the question that it has distinct elemets then why to count frquency
Try with your way and DM me if error comes then definitely share with me
Bhayia why can't we use unordered map? Your algorithm is taking more time than mine.
since we already sorted negative values so what is the need of ordered map , since searching time is more in ordered map? attaching code in reply
class Solution{
public:
vector PosNegPair(int a[], int n)
{
unordered_map umap; vector v;
for(int i=0;i0)
umap[a[i]]++;
else if(a[i]0)
{
ans.push_back(*it);
ans.push_back(-*it);
umap[-*it]--;
}
}
return ans;
}
};
Amazing 🤩🤩
in 27th line of code, while loop should be used ....Here there are distinct elements that's why frequency of all elements is 1 and its working
Keep it up bhaiya
Thanks for the support and love
can anyone give the solution of method 2??I am trying but i am not able to solve completely.
vector PosNegPair(int a[], int n) {
unordered_map posMap;
unordered_map negMap;
for(int i=0;i=0){
posMap[a[i]]++;
}
else{
negMap[a[i]]++;
}
}
vector ans;
//Traverse positive unordered map
for(auto it:posMap){
int ele=it.first*-1;
if(negMap.find(ele)!=negMap.end()){
int mini=min(negMap[ele],it.second);
for(int i=0;i
Hahahaha maza aa raha h zindagi m 😂😂
Bass tum yaar share kardo iss channel ko apne college me , social media me ..jaha pe tunhe thik lagee
Mujhe v mazaa aa jayega zindagi me 😊😊
@@HelloWorldbyprince sure bhaiya 👍😂
Sir xorequality jo codechef ka may challenge mein hai us type ka question ka time complexity kaise reduce karein? optimize way kaise hoga? uss type ka question ko kaise approach karein?
Pattern way me socho
Try karke dekho ki kuch pattern bann raha hai kya ?
@@HelloWorldbyprince brute force hi ghum raha dimag mein pair wla question sabko kaise kam karein time complexity nai samajh aa rha
public List PosNegPair(long a[], long n)
{
Map freq = new HashMap();
for (long i : a) {
freq.put(i, freq.getOrDefault(i, 0) + 1);
}
List pair = new ArrayList();
for (long i : a) {
int cnt = 2 * Math.min(freq.get(i), freq.getOrDefault(-i, 0));
freq.put(-i, 0);
while (cnt-- > 0) {
if (i < 0)
i = -i;
pair.add(i);
}
}
pair.sort(null);
for (int i = 0; i < pair.size(); i += 2) {
pair.set(i, pair.get(i) * -1);
}
return pair;
} // Sir we can do it with a single map also
Achaa Jharkhand me kaha se bhaiya 🔥
Ramgarh aur tum ?
@@HelloWorldbyprince bhaiya mein bhi jharkhand se hun aur apki sari videos dhekhti hu ❤🔥
vector PosNegPair(int a[], int n) {
unordered_mapm;
sort(a,a+n);
for(int i=0;i0){
m[a[i]]++;
}
}
vectorans;
for(int i=n-1;i>=0;i--){
if(a[i]
nice work
2nd Approach Solution :-
class Solution{
public:
vector PosNegPair(int arr[], int n) {
unordered_map pos;
unordered_map neg;
vector ans;
for(int i=0; i0){
pos[arr[i]]++;
}else neg[arr[i]]++;
}
for(auto it=pos.begin(); it!=pos.end(); it++){
int key = it->first;
int value = it->second;
if(neg.find(-key)!=neg.end()){
auto negItr = neg.find(-key);
int negValue = negItr->second;
int data = min(value, negValue);
for(int i=0; i
thanks
15:05
vector PosNegPair(int a[], int n) {
unordered_map mp;
set s;
vector v;
for(int i=0; i0)
s.insert(a[i]);
}
int m;
for(auto a : s){
int x= -(a);
if(mp[a]
good work Anjali
public:
vector PosNegPair(int a[], int n) {
unordered_map umap;
for(int i=0;i
but sorting will take O(nlogn) time complexity
Yes it will , mb @@CodeNinja18
@@CodeNinja18 Consider this an alternative approach ☺️