Subarray with given sum

  Рет қаралды 203,123

Techdose

Techdose

Күн бұрын

Пікірлер: 286
@techdose4u
@techdose4u Жыл бұрын
🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037 🔴 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/
@CodeSuccessChronicle
@CodeSuccessChronicle 3 жыл бұрын
your brain deserves more likes 👏
@techdose4u
@techdose4u 3 жыл бұрын
😅
@mikasaackerman2694
@mikasaackerman2694 4 жыл бұрын
Nice explanation. Isn't it the same question as subarray sum equals k. You have uploaded that video too and that works for every edge case as well as negative numbers.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks
@mohit7717
@mohit7717 9 ай бұрын
Thanks for sharing such a valuable programming content
@madhz007
@madhz007 Жыл бұрын
Your explanation deserves more views
@sujitgoswami4583
@sujitgoswami4583 3 жыл бұрын
NOTE:- I'd recommend you guys to know the logic and then write a simple JAVA,C/C++ code, vector isn't much required. those who are unable to solve it with vector, here's the full solution. :) JAVA:- static void subarraySum(int n, int s, int[] arr) { int start=0,end=0,sum=0, r=0; int i=0; while(is)//if sum becomes greater { start=start+1; sum=0; i=start-1; } if(sum==s)//if we found all index { r++; end=i+1; start++; break; } i++; } if(r>0) System.out.print(start+" "+end); else System.out.print("-1"); } -------------------------------------------------------------------- C++, #include vector subarraySum(int arr[], int n, int s){ int l = 0; int r = 0; int count = 0; vector res; while (r
@techzone0131
@techzone0131 3 жыл бұрын
Bro gfg me ouput wrong bta rha h
@padiakhil7620
@padiakhil7620 2 жыл бұрын
@@techzone0131 lol same, but we need to add another if block if given sum==0 thn return -1
@wiccanmarvelous
@wiccanmarvelous Жыл бұрын
@@padiakhil7620 yes after this condition solution is perfect 😄
@sravan.t7581
@sravan.t7581 Жыл бұрын
why not in python?
@Flux-e4y
@Flux-e4y Жыл бұрын
we cannot return -1 because it is a vector@@padiakhil7620
@braj_marg
@braj_marg Жыл бұрын
thank you so much sir simple and easy explaination! radhavallabh shri harivansh
@K_EC_AushoRoup
@K_EC_AushoRoup 4 жыл бұрын
What if there is more than one subarray whose sum is equal to the target sum. Then how will you modify your code? For reference see LeetCode 1171 problem. I know this 1171 LeetCode can be solved by another approach(I used Stack for solving it). But I want to apply Target sum subarray algorithm here in this question.
@supriyamanna715
@supriyamanna715 Жыл бұрын
then u use a hashmap to store the sums for each windows
@shirish2005
@shirish2005 Жыл бұрын
Nice explaination
@sushobhitjakhmolaAdmin
@sushobhitjakhmolaAdmin 3 жыл бұрын
Hey what if after 10 there is 5. So when we get currentsum as 38 , adding next element -5 would make it as 33. Perhaps it isn't suitable for arrays containing negatives
@tusharnain6652
@tusharnain6652 2 жыл бұрын
yes!
@lazzyboii2573
@lazzyboii2573 2 жыл бұрын
Then to use this algorithm maybe add all the elements with the most negative number so every element will be positive
@Y_AnSHumaan
@Y_AnSHumaan 2 жыл бұрын
You'll have to use an array with running/prefix sum to solve if the arrays also contains negatives.
@motivation8238
@motivation8238 Жыл бұрын
yes it will work only for positive numbers, if you want to handle negatives then you have to use hashmap
@SurajKumar-cg1mm
@SurajKumar-cg1mm 4 жыл бұрын
halo bro what will we do if our first element is greater then sum { 43, 1, 17, 26, 15 } and sum is 32
@anuj4200
@anuj4200 4 жыл бұрын
If first element is greater then increment both right and left pointers.
@techdose4u
@techdose4u 4 жыл бұрын
If you find either right or left element is greater then just skip over that element.
@aryankhandelwal15
@aryankhandelwal15 3 жыл бұрын
@@techdose4u sir u should have mention this.
@tejasdonadkar9094
@tejasdonadkar9094 3 жыл бұрын
Please Explain Longest subarray with given sum using HashSet if possible
@jawwadakhter5261
@jawwadakhter5261 4 жыл бұрын
really .........great explanation,u should keep on uploading like these content.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@ankitchawla8487
@ankitchawla8487 4 жыл бұрын
This was a good explanation thanks a lot for making this video.
@senthilmurugan7304
@senthilmurugan7304 4 жыл бұрын
how to find all possible sub array consider [2,3,5,7,10,15] sum=10 here 7,3 one subset and 2,3,5 also another subset.
@prashantprajul9526
@prashantprajul9526 3 жыл бұрын
7and 3 is not a subarray
@arijitchandra8218
@arijitchandra8218 4 жыл бұрын
is it an implementation of sliding window technique?
@techdose4u
@techdose4u 4 жыл бұрын
Yea
@suyashmisra7406
@suyashmisra7406 4 жыл бұрын
Hello friend,you're doing a great job and I am very thankful for these videos. I had a small doubt regarding your explanation of the time complexity. You said that since we're" passing" through each element a max of two times,the complexity should be O(2*N) which obviously is O(N). I think ,since incrementing left is an O(1) operation, it doesn't cause the time complexity to be O(2*N).It should still be O(N),simply because the loop will run for a max of n times(doing O(1) work each time) . Even though we get the same complexity eventually, getting the analysis wrong might be disastrous in an otherwise highly effective answer.
@jaishreesaraswat2800
@jaishreesaraswat2800 3 жыл бұрын
Even I had the same doubt , did you find its solution?
@phushyamithragauri9258
@phushyamithragauri9258 Жыл бұрын
I guess the loop ends when left and right == n ?? then there is chance of moving twice hence O(2*N).
@San2shsingh
@San2shsingh Жыл бұрын
Can you help me with python script for this
@AgarwalAashi
@AgarwalAashi 3 жыл бұрын
Very nicely explained!
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@sigma7253
@sigma7253 3 жыл бұрын
@@techdose4u what if its starts with -ve array
@abdurrehmanansari9219
@abdurrehmanansari9219 3 жыл бұрын
Copy hai code from geeks for geek site and even variable is same..
@Kushagra_21
@Kushagra_21 4 жыл бұрын
sir your videos has helped a lot . Sir can you tell me from where should i prepare for technical mcqs . I have exam of accolite company next week. Thankyou for the help.
@techdose4u
@techdose4u 4 жыл бұрын
Well for short time, if you have GATE notes then revise DBMS and OS atleast, otherwise, just study important MCQ questions from geeks and search for top 50 and top100 MCQ questions for interviews. That should help :)
@Kushagra_21
@Kushagra_21 4 жыл бұрын
@@techdose4u thanks a lot sir
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@gaurika4927
@gaurika4927 3 жыл бұрын
What is negative elements are also present [4,-1,-2,0,3,1] and sum 1
@memeologicalworld8929
@memeologicalworld8929 Жыл бұрын
But when we need to go upto n-1 then at n-1 we have our sum equal to required sum but loop again run and i=n so,loop ends and there are more problems on this when you are going to submit on gfg,leetcode,etc.🧐
@arpanbarua5424
@arpanbarua5424 Жыл бұрын
Plz type the code aslo❤❤either way nice and good video🎉🎉
@aasifali9139
@aasifali9139 2 жыл бұрын
thx brother it was really helpful....
@for461
@for461 2 жыл бұрын
[1,1,2] --> will fail ? for target 2 should return position 2.
@shubrochakroborty5918
@shubrochakroborty5918 2 жыл бұрын
Bro you are just awesome
@techdose4u
@techdose4u 2 жыл бұрын
Thanks :)
@jordan_heartz
@jordan_heartz 10 ай бұрын
Input nums = [-1,-1,1] k = 0 Use Testcase Output 0 Expected 1 ### This approach will not work for negative numbers
@hmmmsmmsmsmsm
@hmmmsmmsmsmsm 2 жыл бұрын
I came here after code chef long competition find lowest sub array with sum k 😶
@057anmolkesarwani4
@057anmolkesarwani4 3 жыл бұрын
Nice one
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@GdLongerHandle
@GdLongerHandle 4 жыл бұрын
what if sum = 15. right pointer will stop at 2. And till 2 we shall not have any subarray with sum =15
@mayurkoli4145
@mayurkoli4145 4 жыл бұрын
approach not work on array having negative numbers arr : [-1, -1, 1] target = 0, Not a generalize solution.
@techdose4u
@techdose4u 4 жыл бұрын
I have already answered on your comment 6 months back. Please search it in comment section. You asked the same question.
@mayurkoli4145
@mayurkoli4145 4 жыл бұрын
​@@techdose4u ohh sry!!!! : )
@techdose4u
@techdose4u 4 жыл бұрын
No problem :)
@msahai7017
@msahai7017 4 жыл бұрын
Hi...How can we do the same thing using maps??
@askchaitanya97
@askchaitanya97 4 жыл бұрын
What of there is 9 at index 4 and sum is still 33? I mean what if we have subarray sums greater than or lower than the given sum , but not exactly equal to it?
@edenfire1221
@edenfire1221 3 жыл бұрын
Yeah, either we are looking for something else, or this is wrong.. if its 9 at index 4, it wont know the sum of index 0, 2, 3, and 4 = 33.
@LightningXThunderVlogs
@LightningXThunderVlogs 3 жыл бұрын
vector subarraySum(int arr[], int n, int s) // NOTE : array contains non-negative integers { if(s == 0){ // handle sum = 0 for(int i=0; i
@mrsmurf911
@mrsmurf911 2 жыл бұрын
small modification for GFG Practice: return vector{l+1,r}; instead of return vector{l,r-1};
@chrisogonas
@chrisogonas 3 жыл бұрын
Very well explained! Thanks
@siddharthashankar8396
@siddharthashankar8396 2 жыл бұрын
Very Helpful
@vithleshkumargupta
@vithleshkumargupta 4 жыл бұрын
you are doing great work👍👍
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@karanalang1573
@karanalang1573 4 жыл бұрын
nice explanation .. can you pls provide sample code ?
@techdose4u
@techdose4u 4 жыл бұрын
Will try to.
@gopikamurali440
@gopikamurali440 4 жыл бұрын
Pls give code
@selectivitism
@selectivitism 4 жыл бұрын
This solution is so damn freaking clever!
@techdose4u
@techdose4u 4 жыл бұрын
:)
@ecstacist6749
@ecstacist6749 3 жыл бұрын
Thank you ! What if the solution elements are non_consequtive
@PankajSingh-mm3br
@PankajSingh-mm3br 3 жыл бұрын
A subarray is a contiguous part of array. An array that is inside another array
@cuteminired6550
@cuteminired6550 8 ай бұрын
​@@PankajSingh-mm3br thanks
@Usurperhk
@Usurperhk 4 жыл бұрын
What if the left pointer become equal to or exceeds the right pointer??? FIX IT!!!!
@TechDextro
@TechDextro 3 жыл бұрын
what if there are negative elements in the array
@howardlam6181
@howardlam6181 Ай бұрын
what if the subarray does not need to be consecutive numbers. There can be "holes" in the middle. That's you get the minimum number of elements from the array to reach the sum.
@Spark_T_ai
@Spark_T_ai 25 күн бұрын
subarray by definition means contiguous ,, i guess that should be in the description
@howardlam6181
@howardlam6181 25 күн бұрын
@@Spark_T_ai yea it's actually subsequence in that case. It was for a different problem but I figured out the solution anyway.
@c56tejashsharan37
@c56tejashsharan37 9 ай бұрын
it will not work if there will be negative numbers in the subarray
@kashifanwar9733
@kashifanwar9733 4 жыл бұрын
nice approach
@techdose4u
@techdose4u 4 жыл бұрын
:)
@varun8441
@varun8441 4 жыл бұрын
what will be termination condition if we don't find and sum equal to given sum??
@techdose4u
@techdose4u 4 жыл бұрын
Termination will be when right pointer goes out of bounds, that is, when right pointer goes beyond last value in array.
@eeshgadol
@eeshgadol 2 ай бұрын
Code, java , this approach with one loop!: public static ArrayList subarraySum(int[] arr, int n, int s) { ArrayList result= new ArrayList(); int left=0; int right=0; int sum=arr[left]; while(rights) { sum=sum-arr[left]; //System.out.println("sum minus left " + sum); left++; } else if(sum
@gauravborkar1345
@gauravborkar1345 3 жыл бұрын
Excellent
@vivekkumar-fk9lc
@vivekkumar-fk9lc 4 жыл бұрын
But instead of giving the longest subarray, it will return the first subarray having sum equal to k.. How would we get the largest sum
@hersheynohara5887
@hersheynohara5887 4 жыл бұрын
Thanks for the video...
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@chetanpadhen7780
@chetanpadhen7780 Жыл бұрын
Sir If You Also Go Through To The Code,It Will Be More Helpful. Thanks
@techworld3043
@techworld3043 2 жыл бұрын
what software do you use to teach ?
@this.supratim
@this.supratim 4 жыл бұрын
Great explanation 👌
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@inderjeetchawla527
@inderjeetchawla527 4 жыл бұрын
Bhai negative elements bhi ho array me to. And Bhai jab video banao to saare cases cover Kia Karo plz.
@techdose4u
@techdose4u 4 жыл бұрын
Yes you are correct. I did not cover for negative numbers. You can do so by using the modified kadane's algorithm which works for negative numbers as well. Whenever you are given a negative number as sum then negate the sum to make it positive and also negate all the elements of the array and so sum to be found will always be positive and so you can use kadane's algorithm.
@ayushkashyap8299
@ayushkashyap8299 4 жыл бұрын
I think finding all subsets of a array is O(2^n) not O(n^2). Correct me if I am wrong.
@techdose4u
@techdose4u 4 жыл бұрын
There are 2^n possible subsets of an array. So yeah, you are correct. I might have mistakenly told something, my bad.
@indyarockers
@indyarockers 4 жыл бұрын
all subsets of array is O(2^n) but consecutive subsets of an array is just O(n^2).
@secularph8424
@secularph8424 4 жыл бұрын
That's for non continuous Subarray, For continuous Subarray its O(n^2)
@AvinashJ21
@AvinashJ21 3 жыл бұрын
To move pointers we need while loop ?
@mameabera5852
@mameabera5852 2 жыл бұрын
what if the array is [17, 85, 93, -45, -21] and sum = 150 . this doesn't work by your algo
@varun8441
@varun8441 4 жыл бұрын
what will be termination condition if we dont find and sum equal to given sum??
@srajsekhar4061
@srajsekhar4061 4 жыл бұрын
Nice explanation 👌👌👌 bro
@techdose4u
@techdose4u 4 жыл бұрын
Thanks bro :)
@Nikita-hv5jm
@Nikita-hv5jm 3 жыл бұрын
Can we use kdane's algo for this problem?
@techdose4u
@techdose4u 3 жыл бұрын
Try 2 pointer technique.
@shashikumari4000
@shashikumari4000 3 жыл бұрын
What if sum is 9 only 5 and 4 satisfies.... Can someone explain this🙏 how will the algorithm with
@mandeepdas351
@mandeepdas351 3 жыл бұрын
the explanation is about finding subarray and not subsequence. 5 ,4 is not a subarray.
@nabeelahahmed4610
@nabeelahahmed4610 3 жыл бұрын
Can u share the entire code plz? That will be better to understand more properly
@akshayzade9761
@akshayzade9761 3 жыл бұрын
static ArrayList subarraySum(int[] arr, int n, int s) { ArrayList list = new ArrayList(); int left = 0, right=0; int currentsum = arr[0]; try { for(int i=1; i s) { currentsum = currentsum - arr[left]; left +=1; } } else if(currentsum > s) { currentsum = currentsum - arr[left]; left += 1; } } } catch(ArrayIndexOutOfBoundsException e) { list.add(-1); } return list; } Sir might have better code structure solution has this. but mine is also working as per sir approach
@aryankhandelwal15
@aryankhandelwal15 3 жыл бұрын
o bhai bhot hard lg rha tha
@tarzanyt3047
@tarzanyt3047 4 ай бұрын
you are amazing
@leetcoder6569
@leetcoder6569 2 жыл бұрын
Great
@shrimatkapoor2200
@shrimatkapoor2200 4 жыл бұрын
Also, this would only work for continuous sub-arrays would it not?
@techdose4u
@techdose4u 4 жыл бұрын
Subarray is continuous only. If something is not continuous then it is either subsequence or subset. This will work only for subarrays.
@shrimatkapoor2200
@shrimatkapoor2200 4 жыл бұрын
@@techdose4u Cool I think I was looking for the solution for subsets and target sums. Thanks for the clarification.
@snehill7275
@snehill7275 2 жыл бұрын
I am not sure if the TC is O(n) here, because in the worst case we have to move n-1 time backward on that moment TC will n x (n-1) => O(N^2) ex- [1,2,20,10,2], sum = 12 sum_will be like = 1,3,23 12) we go backward 23-1= 22 , 22-2= 20, 20-10=10 as 10 < 2 we can move forward and doing so we will ended up with answer => (3,4) pls, correct me if I am wrong.
@Shanky_17
@Shanky_17 3 жыл бұрын
CAN WE USE THE SAME METHOD IF THERE ARE MORE THAN ONE SUB-ARRAY WITH SUM =K ? and is it sliding window ?
@parthanuj8611
@parthanuj8611 Жыл бұрын
I think that would not be the case when u are solving porgram in leetcode or gfg but yeah it is possible ..
@koushikshomchoudhury9108
@koushikshomchoudhury9108 4 жыл бұрын
What you've shown is called a Sliding Window approach. Unfortunately it's an O(n^2) solution.
@techdose4u
@techdose4u 4 жыл бұрын
You are mistaken. It's O(N) only. How did you conclude O(N^2) ?
@sonusangwan7898
@sonusangwan7898 5 жыл бұрын
great video brother
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@leetcodepythonsolutionsand9396
@leetcodepythonsolutionsand9396 4 жыл бұрын
Can you provide explanation of the DP solution?
@surajsingh-sm7qx
@surajsingh-sm7qx 4 жыл бұрын
U r awesome
@poonamsangale4037
@poonamsangale4037 3 жыл бұрын
Sir you have explained very well but can you please provide us code
@techdose4u
@techdose4u 3 жыл бұрын
I was not providing the code earlier but now I do.
@shouryagupta8763
@shouryagupta8763 3 жыл бұрын
nice sir
@dev.suyash
@dev.suyash 3 жыл бұрын
Sir,Can you please share code for this mentioned approach?
@akshayzade9761
@akshayzade9761 3 жыл бұрын
static ArrayList subarraySum(int[] arr, int n, int s) { ArrayList list = new ArrayList(); int left = 0, right=0; int currentsum = arr[0]; try { for(int i=1; i s) { currentsum = currentsum - arr[left]; left +=1; } } else if(currentsum > s) { currentsum = currentsum - arr[left]; left += 1; } } } catch(ArrayIndexOutOfBoundsException e) { list.add(-1); } return list; }
@vinayak186f3
@vinayak186f3 3 жыл бұрын
Will it work for array with some negative elements ? I think it won't , correct me if wrong
@sigma7253
@sigma7253 3 жыл бұрын
what if we start arr[0] is -ve
@shrimatkapoor2200
@shrimatkapoor2200 4 жыл бұрын
Isn't the number of subsets of a finite set 2^n for n cardinality of the set
@techdose4u
@techdose4u 4 жыл бұрын
You are correct. But here we are solving subarray which is continuous and not subset. So it will be N^2 for subarray.
@rahu1119
@rahu1119 5 жыл бұрын
which presentation software is this ?
@khushijain3574
@khushijain3574 3 жыл бұрын
which software you are using
@ajaygonepuri2285
@ajaygonepuri2285 Жыл бұрын
thank you sir!
@atiquesiddiqui710
@atiquesiddiqui710 4 жыл бұрын
Can you please provide the code for this
@euphoric3464
@euphoric3464 3 жыл бұрын
I don't think this will work for all kind of arrays like a={2,4,6,8,9,10}
@tokirmanva2247
@tokirmanva2247 3 жыл бұрын
This approach won't work if there are negative numbers also.
@comradepb
@comradepb 2 жыл бұрын
Two pointer approach is not good here because the array can have negative numbers.
@sukdipkar8876
@sukdipkar8876 4 жыл бұрын
yaa it is going well but it exceeded time limit.
@ryan-bo2xi
@ryan-bo2xi 4 жыл бұрын
this uses dynamic window technique
@techdose4u
@techdose4u 4 жыл бұрын
Yea :)
@K_EC_AushoRoup
@K_EC_AushoRoup 4 жыл бұрын
Agreed
@akshhay
@akshhay 4 жыл бұрын
Ye contigious hogyi, what if we want any subarray?
@theWrongCode
@theWrongCode 4 жыл бұрын
we changed this problem with array containing multiple subarray with given sum.
@prabhatkashyap8333
@prabhatkashyap8333 4 жыл бұрын
did the same on geeksforgeeks platform, showing TLE.
@techdose4u
@techdose4u 4 жыл бұрын
Should not give TLE bro :( Is there any algo to calculate below O(N) time?
@prabhatkashyap8333
@prabhatkashyap8333 4 жыл бұрын
@@techdose4u i think sliding window, thats what i am also looking for.
@techdose4u
@techdose4u 4 жыл бұрын
@@prabhatkashyap8333 yes correct.
@prabhatkashyap8333
@prabhatkashyap8333 4 жыл бұрын
@@techdose4u ide.geeksforgeeks.org/iN2jy9gvuo check this, and try to submit it on GFG, practice.geeksforgeeks.org/problems/subarray-with-given-sum/0 and tell me the mistake, why TLE?
@steevjames6720
@steevjames6720 10 ай бұрын
What if there are negative numbers ?
@yaswanthreddy372
@yaswanthreddy372 4 жыл бұрын
thanks bro,its working
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@dayagutte45
@dayagutte45 4 жыл бұрын
@@techdose4u plz give me solution of this Write a program to find lowest number of integers in the array that sums up the given number. In java,c,c++ only no python Write a program to find lowest number of integers in the array that sums up the given number. The program should ask user to input array of integers (“Input Array”) and the required sum (“Required Sum”). The output (“Output”) should list the lowest number of integers from the input array that sums up the “Required Sum”. Refer to examples given below. Input : Array of integers Required sum Output : Elements from array which makes sum equals to given value Example: Input Array : [10, 0, -1, 20, 25, 30] Required Sum: 45 Output: [20, 25] Required Sum: 59 Output: [10, -1, 20, 30] Required Sum: 60 Output: [10, 20, 30]
@dayagutte45
@dayagutte45 4 жыл бұрын
Can u provide a code plz
@dayagutte45
@dayagutte45 4 жыл бұрын
Can u provide a code plz
@user-so9jd2nf4s
@user-so9jd2nf4s 2 жыл бұрын
Hey, how to find multiple sub arrays?
@mahesan2881
@mahesan2881 Жыл бұрын
I don't think the second method is correct, what if the first element contains 10000. That is larger than the sum 33, but if r goes one step ahead, then the currsum will not decrease but increase.
@eyasir2047
@eyasir2047 Жыл бұрын
Two pointer approach
@pepetikesavasiddhardha7852
@pepetikesavasiddhardha7852 2 жыл бұрын
simple yet sexy logic....
@piyushverma5797
@piyushverma5797 4 жыл бұрын
find subarray with given sum negative numbers allowed in constant space
@sunnyvarun6652
@sunnyvarun6652 4 жыл бұрын
How will this problem be solved? int arr[]= {1,4,45,6,10,8}, target=11.
@satyamsingh_47
@satyamsingh_47 4 жыл бұрын
This is a subset sum problem which is different from a subarray sum problem
@techie_bhuvi
@techie_bhuvi 3 жыл бұрын
This will fail for negative number [-1,-1,1] expected 1 but this code will return 0
@akp_04
@akp_04 Жыл бұрын
wrong solution. your sol will only work in case when array is sorted. if there are negative numbers and array is not sorted this wont work. you should highlight the constraints in the beginning of the video. nonetheless explanation was good
@neghatnazir1668
@neghatnazir1668 4 жыл бұрын
hey i have a doubt regarding time complexity. e.g if we are given 1 2 5 10 ,sum=10 the second pointer is going to move till n, in this case the time complexity would b O(n^2). i am confused, can you please explain this case
@supratikpadhee4235
@supratikpadhee4235 4 жыл бұрын
to understand tc , always find how many times the particular element is processed . In this case as @tech dose explained , in the worst case scenario , we shall need to process the element 2 times , so total complexity = O(2* n )
@supratikpadhee4235
@supratikpadhee4235 4 жыл бұрын
as per your doubt , it will not be O(n^2) in the efficient approach ? why ? | | -----> window bounded by left and right pointers. | 1| , | 1 , 2| , | 1 , 2 ,5| , | 1 , 2 ,5 , 10| , | 2 ,5 , 10| , | 5 , 10| , | 10| ------> different states of the window in the worst case , our window can take 2n states , i.e increase and then decrease , so at max TC = O(2n).
@neghatnazir1668
@neghatnazir1668 4 жыл бұрын
@@supratikpadhee4235 thanks :)
@neghatnazir1668
@neghatnazir1668 4 жыл бұрын
@@supratikpadhee4235 thanks :) i checked it after
@runtimeError123
@runtimeError123 2 жыл бұрын
It will fail bro in many cases Example [-1, -1, 0] and k=0 . Here he have to count entire array also.
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 335 М.
Arrays Series #4- Find subarray with given sum - Solution Explained in Java
14:44
Code With Ease - By Varsha
Рет қаралды 8 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 12 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 98 МЛН
А я думаю что за звук такой знакомый? 😂😂😂
00:15
Денис Кукояка
Рет қаралды 3,3 МЛН
Молодой боец приземлил легенду!
01:02
МИНУС БАЛЛ
Рет қаралды 1,9 МЛН
Subarray Sum Equals K - Prefix Sums - Leetcode 560 - Python
15:19
Kadanes algorithm | Longest sum contiguous subarray
7:51
Techdose
Рет қаралды 164 М.
Factoring Quadratics WITHOUT Guessing Product & Sum
20:01
JensenMath
Рет қаралды 125 М.
Why Canada’s changing its immigration system
6:53
Justin Trudeau
Рет қаралды 1,1 МЛН
Youtube's Longest Argument is Over
8:38
Deep Humor
Рет қаралды 70 М.
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 487 М.
What Does "Problem With This Drive" Error ACTUALLY Mean?
9:59
PROBLEM OF THE DAY: 19/08/2023 | Subarray with Given Sum | GeeksforGeeks Practice
12:35
Subarray with given Sum | Python Code
11:29
nETSETOS
Рет қаралды 11 М.
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 12 МЛН