Subarray with sum 0 | GeeksforGeeks | Hashing data structure | Leetcode DSA series | Prefix Sum

preview_player
Показать описание
This is the video under the series of DATA STRUCTURE & ALGORITHM in a HASHING Playlist. We are going to solve the problem "Subarray with 0 sum" from geeks for geeks which are solved by using unordered_set in c++

Given an array of positive and negative numbers. Find if there is a subarray (of size at least one) with 0 sum.

Input:
5
4 2 -3 1 6

Output:
Yes

Explanation: 2, -3, 1 is the subarray with sum 0.

We also Provide courses on Competitive Programming and Data structure and Algorithms. Please see our Full Playlist on our Channel.
----------------------------------------------------------------------------------------

----------------------------------------------------------------------------------------

*Unacademy *
INVITATION CODE: HELLOWORLD

BATCHES FOR INTERMEDIATES

BATCHES FOR BEGINNERS

----------------------------------------------------------------------------------------
*Follow me *
----------------------------------------------------------------------------------------

►Our Playlists on:-

------------------------------------------------------------------------

🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟

✨ Tags ✨
how to find Subarray with 0 sum
question asked in Goldman Sachs interview
how to crack Goldman Sachs online test
how to crack Goldman Sachs
off-campus placement
how to learn to code for beginners
Subarray with 0 sum
hashing in data structure
Best Telegram channel for Off-campus Placement drive
hashing in a data structure in Hindi
unordered_set

#hashing #geeksforgeeks #programming
Рекомендации по теме
Комментарии
Автор

00:02 Understanding the importance of the subarray with sum 0 question.
01:28 Finding subarray with sum 0 using hashing
02:47 Understanding memory location in a nutshell
04:15 Creating a function with a loop and 500 silencers for a specific calculation
05:40 Identifying subarrays with sum 0
07:05 Understanding prefix sum and subarrays
08:34 Understanding the concept of subarrays and prefix sum
10:13 Understanding subarrays with sum 0

rohitrajput
Автор

Done !!
unordered_set<int> s;
int sum = 0;
for (int i = 0; i < arr.size(); i++) {
sum += arr[i];

if (s.find(sum) != s.end() || sum == 0 )
return true;
else
s.insert(sum);
}
return false;

maniish-sgh
Автор

Your way of explaining is amazing... thank you so much sir 👏👏

AmberSparrow
Автор

thanks bhaiya for such a good explaination.

satyamgupta
Автор

you great really
explanation is good.

DIVYAGUPTA-wuoy
Автор

class Solution{
public:
//Complete this function
//Function to check whether there is a subarray present with 0-sum or not.
bool subArrayExists(int arr[], int n)
{
//Your code here
unordered_set<int>s;
int currsum = 0;
s.insert(0);
for(int i=0; i<n; i++){
currsum += arr[i];
if(s.find(currsum) != s.end()){
return true;
}
s.insert(currsum);
}
return false;
}
};//code ho gaya sir logic sikhane ke bad hua par

rode_atharva
Автор

THANK YOU BHAIYA, aapki video dekhnese phele solve hi nahi ho paa raha tha

_adityasakpal
Автор

8:43 pe hash me -1 kese aya, it should be 1 ??

memesbyanony
Автор

Thanks a lot....that diagram of presum1==presum2 cleared everything

anweshangoswami
Автор

In this example {-3, 4, -3, -1, 1} when u stopped at interator 3 previous array which is (-3, 4, -3) not having the sum 0

supriyadas
Автор

Why is the result false for this list [200, 100, -13589, -4, 13589], and it should be true?

RafaelCostaBraga
Автор

Sir, how initially we compare s.find with s.end as there is no element inserted???

anujpradhan
Автор

Thanks for explaining this prefix sum concept in ease way 😊

nutankumari
Автор

watched 2 time because of variable name prefSum1 and prefSum2... 6:33 .. you said ki "vaule aaya one" .. overall explanation was very good..

akarshitgupta
Автор

bool subArrayExists(int arr[], int n)
{
//Your code here
unordered_map<int, int>mp;
for(int i=1;i<n;i++)
{
arr[i]=arr[i]+arr[i-1];
}
for(int i=0;i<n;i++)
{
mp[arr[i]]++;
}

for(auto it:mp)
{
if(it.first==0)
{
return true;
}
else if(it.second>1)
{
return true;
}
}
return false;
}
};

masumali
Автор

//Why this is not working

class Solution{
public:
//Complete this function
//Function to check whether there is a subarray present with 0-sum or not.
bool subArrayExists(int arr[], int n)
{
map <int, int> mp;
mp[0]++;
long long int sum = 0;
for(int i=0;i<n;i++){
sum = sum + arr[i];
mp[sum]++;
if(mp[sum]==2){
return true;
}
}
return false;
}
};

champofseas
welcome to shbcf.ru