Count Pairs With Given Sum | Array Interview

SdĂ­let
VloĹžit
  • čas přidĂĄn 22. 03. 2022
  • JOIN ME
    -----
    CZcams 🎬 / @cppnuts
    Patreon 🚀 / cppnuts
    COMPLETE PLAYLIST
    ------------
    C++ Tutorial For Beginners: • Introduction To C++
    STL (Standard Template Library): • STL In C++
    ThreadIng In C++: • Multithreading In C++
    Data Structures: • Data Structure
    Algorithms: • Binary Search
    Design Patterns: • Factory Design Pattern...
    Smart Pointers: • Smart Pointer In C++
    C++14: • Digit Separator In C++
    C++17: • std string_view in C++...
    C++ All Type Casts: • static_cast In C++
    INTERVIEW PLAYLIST
    ------------
    C++ Interview Q&A: • Structural Padding & P...
    C++ Interview Q&A For Experienced: • How delete[] Knows How...
    Linked List Interview Questions: • Find Kth Node From Bac...
    BST Interview Questions: • Search Element In Bina...
    Array Interview Questions: • Reverse An Array
    String Interview Questions: • Check String Is Palind...
    Bit Manipulation Questions: • Find Set Bit In Intege...
    Binary Tree Interview Question: • Invert Binary Tree
    Sorting Algorithms: • Bubble Sort
    C++ MCQ: • Video
    C MCQ: • What printf returns af...
    C Interview Questions: • Designated Initializat...
    QUICK SHORT VIDEOS
    -------------
    C++ Short : • C++ Short Videos
    C Short : • Shorts C Programming MCQ
    Time Lines
    =========
    0:05 Introduction
    2:19 Brute Force Solution
    3:21 Best Solution
    Count pairs with given sum is one of the array interview question, I have explained both the brute force and the best way to solve it.
    First approach can be using two different loops and just have the comparison of all the pairs possible.Second approach or the best approach is to use an unordered_map and keep the record of occurrences.
    #array #dsa #datastructure #Algorithms

Komentáře • 14

  • @MuthuKumar-vl8of
    @MuthuKumar-vl8of Před 2 lety +7

    I think, in unordered_map you wrongly added -1 instead of 7

  • @FANSasFRIENDS
    @FANSasFRIENDS Před 2 lety

    Good speed and voice, keep it up!

  • @ergingokkaya8323
    @ergingokkaya8323 Před 2 lety

    Excellent

  • @nquanta1548
    @nquanta1548 Před 2 lety

    👍nice

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

    I think -1 entry will not be there . implemeted this
    int main(void)
    {
    int sum = 6,count = 0;
    vector v = {1,5,7,1,1};
    unordered_map m;
    for ( auto i = 0 ; i < v.size(); i++)
    {
    if ( m.find(sum-v[i]) == m.end() )
    {
    m.insert(make_pair(v[i],1));
    }
    else
    {
    count = count + m[sum-v[i]];
    m.insert(make_pair(v[i],1));
    }
    }
    for ( auto i : m)
    cout

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

      Hi guys sorry for the confusion, you guys are correct, i don't know why i said that that doesn't make sense to put (sum-Arr[i]) in umap, i wanted to tell that 7 will be entered in the umap.

    • @its_dsa
      @its_dsa Před 2 lety

      @Suraj Kumar modified code for better modularity :
      #include
      #include
      #include
      using namespace std;
      vector v = {1,1,1,1};
      int Findpairsum(int sum)
      {
      int count = 0;
      unordered_map m;
      for ( auto i = 0 ; i < v.size(); i++)
      {
      if ( m.find(sum-v[i]) == m.end() )
      {
      m.insert(make_pair(v[i],1));
      }
      else
      {
      count = count + m[sum-v[i]];
      m.insert(make_pair(v[i],1));
      }
      }
      for ( auto i : m)
      cout

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

      Output should be 6 not 3.
      As i explained in video..

    • @unoshihaam
      @unoshihaam Před 13 dny

      @@CppNutsi have tried to implement algo so that the output for arr = {1,1,1,1} is 6 but all the time i am getting three . sir can u please give the logic to implement the code

  • @AmitKumar-tp4fv
    @AmitKumar-tp4fv Před 2 měsĂ­ci

    #include
    // //[1,7,3,5,4]
    // using namespace std;
    // void countsum(int arr[],int n,int target)
    // {
    // int cnt=0;
    // setmp;
    // for(int i=0;iarr[i];
    // }
    // int target;
    // cin>>target;
    // countsum(arr,n,target);
    // }