Count Pairs With Given Sum | Array Interview
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
I think, in unordered_map you wrongly added -1 instead of 7
Good speed and voice, keep it up!
Thanks man
Excellent
đnice
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
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.
@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
Output should be 6 not 3.
As i explained in video..
@@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
#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);
// }