Pairs with Positive Negative values | GeeksforGeeks | Hashing data structure | Leetcode DSA series

Sdílet
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

Komentáře • 73

  • @masumali8356
    @masumali8356 Před rokem

    you are the best........masum.

  • @salmaniproductions1104

    Thank you so much

  • @pratik.784
    @pratik.784 Před rokem

    Amazing

  • @aasthajain92
    @aasthajain92 Před 11 měsíci

    Awesome videos

  • @dudlavarumakanth4671
    @dudlavarumakanth4671 Před 3 lety +5

    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

  • @amitraj-vu5oe
    @amitraj-vu5oe Před 2 lety +2

    your video and explanation is great !!! but u deserve more views nd subscriber.... thank u for this video

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +3

      Thanks for the support
      Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @mayankkumar6628
    @mayankkumar6628 Před 2 lety +1

    KHUD SOLVE KARLIYA BHAI CONFIDENCE INFINITTY PAR HAI LOVE FROM UTTARAKHAND

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      Waoooooooooooooooooooooooooooooooo 😊
      Please, if possible then share this channel in your college, groups, or LinkedIn bcoz it's cost nothing to you 😀

  • @oqant0424
    @oqant0424 Před rokem

    14/18 DONE (9.12.22)

  • @Man_of_Culture.
    @Man_of_Culture. Před 3 lety +2

    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;

    • @avibirla9863
      @avibirla9863 Před 2 lety +1

      Can u explain your insert operation ..why you used res.begin() there and not only a[j] and a[i] .?????

    • @memefulland7140
      @memefulland7140 Před 2 lety +1

      nice aproach but we have to solve it by map to understand map more

    • @memefulland7140
      @memefulland7140 Před 2 lety +1

      @@avibirla9863 agr y nhi kia to order shi nhi bnega u should dry run it

    • @itsaayush7357
      @itsaayush7357 Před 10 měsíci +1

      Please tell here what is res.begin() will store?

  • @manojgollapelli3026
    @manojgollapelli3026 Před rokem +4

    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

  • @oqant0424
    @oqant0424 Před rokem

    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;
    }
    };

  • @mayankgarg4157
    @mayankgarg4157 Před 3 lety +2

    bhaiyaa😍😍😍mjaa aagyaa

  • @arvindkathare13
    @arvindkathare13 Před 3 lety +1

    Keep it up bhaiya ji..🙏🏻

  • @amanmotghare7196
    @amanmotghare7196 Před rokem

    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

  • @uditsethi2578
    @uditsethi2578 Před 2 lety +2

    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.

  • @pragatikumari306
    @pragatikumari306 Před 3 lety +1

    m[data ] represent here ....in last few vedio u used either find method or count method

  • @yash2399
    @yash2399 Před 2 lety +3

    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.

  • @harshexploring4922
    @harshexploring4922 Před 2 lety

    Thanks AA lot bhaiya
    This playlist is very helpful
    Love from iitkgp

  • @shashankkumar2534
    @shashankkumar2534 Před 2 lety +2

    its already written in the question that it has distinct elemets then why to count frquency

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      Try with your way and DM me if error comes then definitely share with me

  • @prabhatmishra5667
    @prabhatmishra5667 Před 2 lety +2

    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

    • @prabhatmishra5667
      @prabhatmishra5667 Před 2 lety +1

      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;
      }
      };

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +1

      Amazing 🤩🤩

  • @07rockzz92
    @07rockzz92 Před 2 lety

    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

  • @sohebshaikh3344
    @sohebshaikh3344 Před 2 lety

    Keep it up bhaiya

  • @keshavagarwal5900
    @keshavagarwal5900 Před 2 lety

    can anyone give the solution of method 2??I am trying but i am not able to solve completely.

    • @gaurimandot5788
      @gaurimandot5788 Před rokem

      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

  • @avibirla9863
    @avibirla9863 Před 2 lety +1

    Hahahaha maza aa raha h zindagi m 😂😂

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 2 lety +2

      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 😊😊

    • @avibirla9863
      @avibirla9863 Před 2 lety

      @@HelloWorldbyprince sure bhaiya 👍😂

  • @atulkumarsinha7765
    @atulkumarsinha7765 Před 3 lety

    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?

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před 3 lety

      Pattern way me socho
      Try karke dekho ki kuch pattern bann raha hai kya ?

    • @atulkumarsinha7765
      @atulkumarsinha7765 Před 3 lety

      @@HelloWorldbyprince brute force hi ghum raha dimag mein pair wla question sabko kaise kam karein time complexity nai samajh aa rha

  • @aritrakumarbara5994
    @aritrakumarbara5994 Před 5 měsíci

    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

  • @NikhilKumar-hr5qt
    @NikhilKumar-hr5qt Před 3 lety +1

    Achaa Jharkhand me kaha se bhaiya 🔥

  • @Ramneet04
    @Ramneet04 Před rokem

    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]

  • @sambhavsharma2780
    @sambhavsharma2780 Před 2 lety +3

    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

  • @AnjaliSingh-yk9yc
    @AnjaliSingh-yk9yc Před rokem

    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]

  • @316_mohammadahadkhan7

    public:
    vector PosNegPair(int a[], int n) {
    unordered_map umap;
    for(int i=0;i

    • @CodeNinja18
      @CodeNinja18 Před 18 hodinami +1

      but sorting will take O(nlogn) time complexity

    • @ahadkhan8872
      @ahadkhan8872 Před 17 hodinami

      Yes it will , mb ​@@CodeNinja18

    • @ahadkhan8872
      @ahadkhan8872 Před 17 hodinami

      ​@@CodeNinja18 Consider this an alternative approach ☺️