Thanks anuj bhaiya. You are building a great cornerstone for students. In between how to learn kubernetes and how to start learning devops. Could you please tell about this?
very well explained... i cam across this question while searching for another similar but a littile more complex... can you please help solve.. The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i
There can be multiplie subarray but here we need to print any one If question is find all possible subarray with given sum ,then your ans will also be included.
@@vikashkumarbhagat3001 But what he told will not work for the largest subarray. In the example the largest subarray with sum 5 would be [-5, 15, -10, 5], but if we do it according to what he said largest subarray is [15, -10].
Hey anuj. Thanks for the great content. Tried doing the largest subarray with equal 0,1 problem as mentioned at the end of this video. I think its not a variant to this one and would be solved with a different logic altogether. As if we do find subarray with sum zero, the algorithm of this video will find the LAST subarray with sum zero and not the LARGEST subarray with sum zero. If I'm right, please do cut the end part of this video.
Bhaiya please focus and complete the DSA-One series🙏🏻 make your DSA-One series, better than Apna College, content wise. Because their content is very vast and lots of different questions, but explanation is not good. You're the god of explaining things❤️❤️ Ps- till now your content of Dsa-One is 🔥❤️
I faced many problems in this video, If there is only 1 element then it will not work or if the sum is 0 then every time currSum - sum will hold true because your sum is zero, this means you are basically checking if currSum is present or not, spoiler alert it's always present because you are putting currSum. Hope you see this and no hate towards you, what you are doing is very helpful for students like me
i replaced the sum with 0 and transformed the array means replaced 0 with -1 and saw that the result is not accurate input={1,1,0,1,1,0,0} output - 3,6 Could you please check the code once @Anuj Bhaiya
``` Python: ``` arr = [1, 1, 0, 1, 1, 0, 0] cur = maxi = 0 d = {0:-1} for i in range(len(arr)): if arr[i] == 1: cur += 1 else: cur -= 1 if cur in d: maxi = max(maxi, i - d[cur] + 2) d[cur] = i print(maxi)
I did't get one thing, why are we storing the curr sum as key and index as values in HashMap. Shouldn't we store the index as key and curr sum as values.
its key value pair so if we have to set value which we want to retrive. here we want index number as output so our index should be value and key will be currsum
We can't find all subarray by using 2 nested for loop as you mentioned at( 2:10). We have to use 3 nested for loop to find all subarray in brute force method.
how to find the largest length , if map keys are(sum) and values are index, when the sum is repeated the index got updated and we can't get actual length when we find the first occurrence of cursum-sum we get updated index of cursum-sum
my bad just do ------------------------- if(!mp.containsKey((sum))) mp.put(sum,i); -------------------------- while putting elements it will not update the keys values
not working in 0 & 1 .. bcz if map contains repetive sum then if we want to update start then it will look for last inserted value in our case we want first inserted value
Nice explanation, what if I have 0 in the array value for the first question? In C# Dictionary keys should be unique right what if I found duplicate keys? will then also this algorithm work?
Best explanation for this technique I have found. The diagram helped a lot. I was struggling to understand this one for days.
Sir you teach the best!! I was stuck on this problem since past 2 days and now it's all clear. Thank you for this amazing content.
can we solve this problem by kadanes algo?
Adding variations to the problem statement is absolutely marvellous 👍
Anuj Bhaiya ,You are the best educator for me🙏🙏💐
Take love & respect from West Bengal 💙
God bless you Bhaiya 😇
Nice to see practical implementation of hash map.
OP Logic Building!
Able to think and code in correct way. Thanks a lot!
Bhaiya aapke videos se bohot acche se samaj aaraha hai thnank you.
Valuable information... extraordinary teacher.. 💥💥
Thanks anuj bhaiya. You are building a great cornerstone for students. In between how to learn kubernetes and how to start learning devops. Could you please tell about this?
Best lecture on Hashmap 😍
Great explaination bhaiya 🙏🙏
well explained. Thanks!
very well explained... i cam across this question while searching for another similar but a littile more complex... can you please help solve..
The Question is:- Given an array of integers of size N, count all possible distinct triplets whose sum is exactly divisible by given integer K. for triplets i, j, k --> i
Also one single element is also part of the subarray, so the last element '5' is also part of the subarray;
There can be multiplie subarray but here we need to print any one
If question is find all possible subarray with given sum ,then your ans will also be included.
@@vikashkumarbhagat3001 But what he told will not work for the largest subarray. In the example the largest subarray with sum 5 would be [-5, 15, -10, 5], but if we do it according to what he said largest subarray is [15, -10].
gajab bhaiya 🔥🔥 very helpful content
Thank You So Much Anuj Bhaiya.................🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
Hey anuj. Thanks for the great content.
Tried doing the largest subarray with equal 0,1 problem as mentioned at the end of this video.
I think its not a variant to this one and would be solved with a different logic altogether.
As if we do find subarray with sum zero, the algorithm of this video will find the LAST subarray with sum zero and not the LARGEST subarray with sum zero.
If I'm right, please do cut the end part of this video.
Wow bhaiya , Great explaination :D
Thankyou so much
Well explain thanks bhaiya👍
very helpful
Best explanation sir 🎉❤
well done, Anuj Bhaiya... thanks!
Very Nicely Explained Sir.
Bhaiya please focus and complete the DSA-One series🙏🏻 make your DSA-One series, better than Apna College, content wise. Because their content is very vast and lots of different questions, but explanation is not good. You're the god of explaining things❤️❤️
Ps- till now your content of Dsa-One is 🔥❤️
Ek dam sahi bola bhai
❤
wo scripted hai esliye 😁😁
Exactly @SURESH KUMAR 😂😂😂😂😂😂
Excellent Brother you made my day
You explained it really well
OSM 👍👍
Really helpful bhaiya !!!! ❤
Thank you Bhaiya
Imp point 09:22
Bhaiya...it will be helpful if u provide some variation problems on every lecture...like u did here ( few more )❤️
Perfect I have gone thorugh multiple videos bit none of them as compared to your solution
thanks for explaining
Brute force approach
int arr1[]={10,5,6,1,8,5,2};
for(int i=0;i
Are bhai tu ekde....😁
bro this was bomb of a video. gold content really!
Thanks Bhaiya For This Efforts 😃😃😃😃
thanks, it is really helpful
Amazing video mate. Thank you so much.
Thanks
There is one more variation, that you have to count the num of subarrays.
yes it was helpful
1st like❤️
I faced many problems in this video, If there is only 1 element then it will not work or if the sum is 0 then every time currSum - sum will hold true because your sum is zero, this means you are basically checking if currSum is present or not, spoiler alert it's always present because you are putting currSum. Hope you see this and no hate towards you, what you are doing is very helpful for students like me
any solution for sum =0 ?
i spend my all day with anuj bhaiya, shradha didi, and geeksforgeeks
I don't understand his language but i still passed my test because of his explanation that i couldn't find in English videos.
Very informative video bhaiya
First time smjh m hi nhi aaya 🤣
Second time ek ek shabd samjh m aaaya♥️😁🙏🏼
We can also use the concept of Sliding Window to solve this problem without using extra Space in the 1st Problem.
How can we use sliding window in case of -ve elements
@@vipulchawla5466 yes I dont think we can use sliding window for -ve
-ve elements ma nahi valid hoga
Anuj bhaiya op 💯💯💯
V nice explanation
Another way of handling that special case is to add a entry in HashMap - map[sum] = -1
Helpful :)
O Man you doing great 👍
i replaced the sum with 0 and transformed the array means replaced 0 with -1 and saw that the result is not accurate
input={1,1,0,1,1,0,0}
output - 3,6
Could you please check the code once @Anuj Bhaiya
``` Python: ```
arr = [1, 1, 0, 1, 1, 0, 0]
cur = maxi = 0
d = {0:-1}
for i in range(len(arr)):
if arr[i] == 1: cur += 1
else: cur -= 1
if cur in d:
maxi = max(maxi, i - d[cur] + 2)
d[cur] = i
print(maxi)
Where is it inaccurate? Unable to find it.
Maja aaya!💚
we can also use 2 pointers technique
Amazing ♥️
best teacher 😊
bhaiya 500k aaj ho jayega, aap congratulations.!
aap apna course mujhe de do na. plz wo java development wala
I did't get one thing, why are we storing the curr sum as key and index as values in HashMap. Shouldn't we store the index as key and curr sum as values.
its key value pair
so if we have to set value which we want to retrive.
here we want index number as output so our index should be value and key will be currsum
in this given example, we have two sub arrays whose sum equals to 20. So I think we need to store all such pairs.
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
@@whoasadkhan map[cursum-sum]+1;
We can't find all subarray by using 2 nested for loop as you mentioned at( 2:10). We have to use 3 nested for loop to find all subarray in brute force method.
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
@@whoasadkhan hey you can write like this
start=m[curr_sum-k]+1;
Thankyou ...🙏
nice understand
YES, IT WAS HELPFUL
Thanks man . This was so good!
my bruteforce approach (155/165 cases passed) :
vector subarraySum(vectorarr, int n, long long s)
{
// Your code here
vector v;
int sum = 0;
for(int i = 0; i < n; i++) {
for(int j = i; j < n; j++) {
sum += arr[j];
if(sum > s) {
break;
}
else if(sum == s) {
v.push_back(i + 1);
v.push_back(j + 1);
return v;
}
}
sum = 0;
}
v.push_back(-1);
return v;
}
the hashmap solution is showing TLE (133/165 cases passed), anyone correct me.
vector subarraySum(vectorarr, int n, long long s)
{
unordered_map u;
int sum = 0, index = 0;
for(int i = 0; i < n; i++) {
sum += arr[i];
u[sum] = index++;
if(u.count(s)) {
v.push_back(1);
v.push_back(i + 1);
break;
}
if(u.count(sum - s)) {
v.push_back(u[sum - s] + 2);
v.push_back(i + 1);
break;
}
}
if(v.size() == 0) {
v.push_back(-1);
return v;
}
return v;
}
Maza aagya bhaiya,,,
Instead of that special case of currsum-sum==0 you could simply add map.put(0,1) right before the loop
you mean (0,-1)?
@@ajay2552 Kuch bhi karo kya farak padta hai
@@subhamdudheria9523 are you sure? Kyuki jab i+1 print hoga.. tab 0 print hona chaiye.. but 2 ho jaega
@@ajay2552 Start aur end variable Lelemge
how to find the largest length , if map keys are(sum) and values are index, when the sum is repeated the index got updated and we can't get actual length when we find the first occurrence of cursum-sum we get updated index of cursum-sum
17 15
-13 0 6 15 16 2 15 -12 17 -16 0 -3 19 -3 2 -9 -6
for this test case k=15
my bad
just do
-------------------------
if(!mp.containsKey((sum)))
mp.put(sum,i);
--------------------------
while putting elements
it will not update the keys values
@@643kanavguleria9 Thanks man!!
not working in 0 & 1 ..
bcz if map contains repetive sum then if we want to update start then it will look for last inserted value
in our case we want first inserted value
thank you bhaiya the way you explain logic it's so simple and good that anyone can get it❤️❤️❤️
Helpfull
I think we can do this sum using sliding window technique also.
Sir subarray with sum equal to zero with three elements taken in pairs bhi isse ho jaega??
1 st one to comment bhaiya
This one is not really working when the sum of the sub array is negative. Is there something I am missing?
THANKYOU SOOOOO MUCH BHAIYA
Please can any1 tell me any other optimum approach which does not use hash map?
I was stuck at this question in the past interview I wish I found it sooner 😑
Just one correction,
add map.put() in else block ->>
else {
map.put(currtSum, i);
}
Can we also solve this Kadane algorithm
I think this code will give the first subarray only, what if we have to find all subarray/number of subarray which is equal to given sum ??
Just wondering if this works in case of a zero element in the array?🤔
Nice explanation, what if I have 0 in the array value for the first question? In C# Dictionary keys should be unique right what if I found duplicate keys? will then also this algorithm work?
Can anyone please explain me ....
In C++
How to write thi line ....
Start = map.get(cursum-sum)+1;
whahh yaar internet ke fayede toh bhot hai
why you used end as -1 not 0???
Bhaiya dsa ki video jadli jaldi dal do🙂🙂🙂
Yesss
Ha❤
Sliding window laga sakte hai na ?
How to write this code in c++
Noice question
Please add questions list for each video of DSA course. Enough to crack tech giant's
3,2,3
sum=6 ..
its returning 0,-1;
but actual answer must be 0,2
bhaiya 12.30 se 13.00 calculaton thoda mistake ho gya ...if change that part it is beneficial for absolute biginer
How would be 2indexing
Hi anuj..
Grt explanations..i tried one example {2, 12, 5} sum =7
Why it is returning false?
Coz no subarray exist with sum 7
See subarray is continuous elements present is a array....i guess u are confused between subarray and subsequence
#thanks
I am learning web development and having too much problems in making logics in JavaScript please give any advice
You will have to solve some basics questions
What kind of ques and wher am i gonna get these que
@@raziahmad8779 please tell
Typical th per logical th practice karta rahunga bhaiyaa
Are your batches on un-academy still taking new enrollments.
No, the enrolment has been closed.
@@AnujBhaiya Any new batches coming up...
Bhaiya plz any list or sheet to practice or follow for dsa after ur vids???
n value is missing, n=a.length;