Find subarray with given sum | Set 2 (Handles Negative Numbers) | GeeksforGeeks

preview_player
Показать описание


This video is contributed by Shubham Kumar

Please Like, Comment and Share the Video among your friends.

Follow us on Facebook:

And Twitter:

Also, Subscribe if you haven't already! :)
Рекомендации по теме
Комментарии
Автор

You have read out loud what was written in the webpage. No explanation whatsover!

gyrogojo
Автор

The intuition behind this algorithm can be assumed as a " sum increasing in frames ", where each element of the array adds to the frame.
the variable CheckSum is increasing with every element.
So, if the scenario that a SUM exists such that a sequence in the array adds to SUM then considering an example where in an array of size 6, IF elements(starting with index 0) at position 2, 3 and 4 add to SUM, then at the iteration when we add arr[4] to checksum(obtained by adding elements till index 3) checksum = arr[0] + arr[1] + arr[2] +arr[3]+ arr[4] but since arr[2] +arr[3]+ arr[4] = SUM, so checkSum = arr[0] + arr[1] + SUM .
or to say CheckSUM = previously obtained CheckSUm(till element arr[1] in this example ) + sum . SInce we keep a track of the previously obtained CheckSUm in the map, we see how far the CheckSUm has come away from the SUM i.e CHeckSUm - Sum . This how far is the previously obtained and hashed Checksums .
the example i used for the dry run, which is better than this video's example for understanding the algorithm is( first example on geeksforgeeks )
Input: arr[] = {1, 4, 20, 3, 10, 5}, sum = 33
Ouptut: Sum found between indexes 2 and 4

suvenitangnu
Автор

Yes he read out like a news reader..All Shubham Kumar's videos are like this..who ever maintaining this channel should not allow further videos by him.. where as other folks did good job..

jhannyvivek
Автор

do this and do that you will get the answer, even the blog post doesn't show the intuition behind coming up with the efficient algorithm.

kishoresrinivas
Автор

I came here to understand the algorithm actually works, but i just heard the mere dictation of what was written in text..

HimanshuSharma-jvpr
Автор

The approach is simple:::

let the given sum be "sumToFind"
1. Iterate over all elements
1.1 Store the total sum of each element till now in variable "cur_sum" and store "cur_sum" in the map, with "cur_sum" as "key" and "index of element" as "value".
1.2 Now let "sum_already_found" = "cur_sum"-"sumToFind" (i.e = "cur_sum")
1.2.1 If "sum_already_found" exists in the map :
then index of ""sum_already_found"+1" to index of "cur_sum" is the answer.
return;
1.2.2 If "sum_already_found" does not exists in the map :
continue;


2. If code reaches here
solution does not exists


-The idea is we are checking if = "cur_sum"

indrasensingh
Автор

Every one is a programmer just read out loud the content of the page.

sahilmalik
Автор

Dude please leave a good explanation. When I click on a video from GFG I expect you guys to explain the APPROACH not read the code and the text. I know English, I can read it myself . I just need help UNDERSTANDING what is written.

ankitchawla
Автор

I expected you would elaborate the solution as written on the gfg webpage..
you just read it like a newspaper...
this isn't a way to explain something

shubhampal-yyrc
Автор

Please explain the logic of O(n) solution with example, if possible.!

vatsaljagani
Автор

Sir, i apprriciate your efforts, but I can't understand you just read only

mohammadsaquibabbas
Автор

Well try, but you can do this problem is O(1) space

ujjvalsharma
Автор

DO ONE THING . Don't waste your time and ours in making such videos which do nothing but read the geeksforgeeks article as it is. The number of dislikes should reassure this .

natesh
Автор

Explain clearly bro. Why r u rushing through the whole concept?

UTKARSHKRISHAKBCE
Автор

Why create a video if you're just reading the GeeksforGeeks article?

shahnawazali
Автор

Why don't you emphasise on solving the problem rather than reading out the content. I have seen this in most of your videos, how do we arrive at a solution is more important to end users than solving the problem.

ss
Автор

unordered_map<int, int=""> mp;
int pre_sum = 0;
int res = 0;

for(int i = 0; i < n; i++)
{
pre_sum += arr[i];

if(pre_sum == sum)
{
res++;
}

if(mp.find(pre_sum - sum) != mp.end())
{
res += mp[pre_sum - sum];
}

mp[pre_sum]++;
}

return res;

wecan
Автор

Questions like these require proper explanation. What's provided in video above is just a read out. Even though the algorithm is correct, its not helping anyone.

parvez
Автор

How to do this question using C also attach code with it

shredder_plays
Автор

Instead of code rundown try making videos where you solve problem with
what you thought when you first saw the problem i.e. with the complete
thought process..any fool who can read code will understand what is
going on in the code no need a vdo for that

prachurjyabasistha