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.
@techdose4u4 жыл бұрын
Thanks
@mohit77179 ай бұрын
Thanks for sharing such a valuable programming content
@madhz007 Жыл бұрын
Your explanation deserves more views
@sujitgoswami45833 жыл бұрын
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
@techzone01313 жыл бұрын
Bro gfg me ouput wrong bta rha h
@padiakhil76202 жыл бұрын
@@techzone0131 lol same, but we need to add another if block if given sum==0 thn return -1
@wiccanmarvelous Жыл бұрын
@@padiakhil7620 yes after this condition solution is perfect 😄
@sravan.t7581 Жыл бұрын
why not in python?
@Flux-e4y Жыл бұрын
we cannot return -1 because it is a vector@@padiakhil7620
@braj_marg Жыл бұрын
thank you so much sir simple and easy explaination! radhavallabh shri harivansh
@K_EC_AushoRoup4 жыл бұрын
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 Жыл бұрын
then u use a hashmap to store the sums for each windows
@shirish2005 Жыл бұрын
Nice explaination
@sushobhitjakhmolaAdmin3 жыл бұрын
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
@tusharnain66522 жыл бұрын
yes!
@lazzyboii25732 жыл бұрын
Then to use this algorithm maybe add all the elements with the most negative number so every element will be positive
@Y_AnSHumaan2 жыл бұрын
You'll have to use an array with running/prefix sum to solve if the arrays also contains negatives.
@motivation8238 Жыл бұрын
yes it will work only for positive numbers, if you want to handle negatives then you have to use hashmap
@SurajKumar-cg1mm4 жыл бұрын
halo bro what will we do if our first element is greater then sum { 43, 1, 17, 26, 15 } and sum is 32
@anuj42004 жыл бұрын
If first element is greater then increment both right and left pointers.
@techdose4u4 жыл бұрын
If you find either right or left element is greater then just skip over that element.
@aryankhandelwal153 жыл бұрын
@@techdose4u sir u should have mention this.
@tejasdonadkar90943 жыл бұрын
Please Explain Longest subarray with given sum using HashSet if possible
@jawwadakhter52614 жыл бұрын
really .........great explanation,u should keep on uploading like these content.
@techdose4u4 жыл бұрын
Thanks :)
@ankitchawla84874 жыл бұрын
This was a good explanation thanks a lot for making this video.
@senthilmurugan73044 жыл бұрын
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.
@prashantprajul95263 жыл бұрын
7and 3 is not a subarray
@arijitchandra82184 жыл бұрын
is it an implementation of sliding window technique?
@techdose4u4 жыл бұрын
Yea
@suyashmisra74064 жыл бұрын
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.
@jaishreesaraswat28003 жыл бұрын
Even I had the same doubt , did you find its solution?
@phushyamithragauri9258 Жыл бұрын
I guess the loop ends when left and right == n ?? then there is chance of moving twice hence O(2*N).
@San2shsingh Жыл бұрын
Can you help me with python script for this
@AgarwalAashi3 жыл бұрын
Very nicely explained!
@techdose4u3 жыл бұрын
Thanks :)
@sigma72533 жыл бұрын
@@techdose4u what if its starts with -ve array
@abdurrehmanansari92193 жыл бұрын
Copy hai code from geeks for geek site and even variable is same..
@Kushagra_214 жыл бұрын
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.
@techdose4u4 жыл бұрын
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_214 жыл бұрын
@@techdose4u thanks a lot sir
@techdose4u4 жыл бұрын
Welcome :)
@gaurika49273 жыл бұрын
What is negative elements are also present [4,-1,-2,0,3,1] and sum 1
@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 Жыл бұрын
Plz type the code aslo❤❤either way nice and good video🎉🎉
@aasifali91392 жыл бұрын
thx brother it was really helpful....
@for4612 жыл бұрын
[1,1,2] --> will fail ? for target 2 should return position 2.
@shubrochakroborty59182 жыл бұрын
Bro you are just awesome
@techdose4u2 жыл бұрын
Thanks :)
@jordan_heartz10 ай бұрын
Input nums = [-1,-1,1] k = 0 Use Testcase Output 0 Expected 1 ### This approach will not work for negative numbers
@hmmmsmmsmsmsm2 жыл бұрын
I came here after code chef long competition find lowest sub array with sum k 😶
@057anmolkesarwani43 жыл бұрын
Nice one
@techdose4u3 жыл бұрын
Thanks
@GdLongerHandle4 жыл бұрын
what if sum = 15. right pointer will stop at 2. And till 2 we shall not have any subarray with sum =15
@mayurkoli41454 жыл бұрын
approach not work on array having negative numbers arr : [-1, -1, 1] target = 0, Not a generalize solution.
@techdose4u4 жыл бұрын
I have already answered on your comment 6 months back. Please search it in comment section. You asked the same question.
@mayurkoli41454 жыл бұрын
@@techdose4u ohh sry!!!! : )
@techdose4u4 жыл бұрын
No problem :)
@msahai70174 жыл бұрын
Hi...How can we do the same thing using maps??
@askchaitanya974 жыл бұрын
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?
@edenfire12213 жыл бұрын
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.
@LightningXThunderVlogs3 жыл бұрын
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
@mrsmurf9112 жыл бұрын
small modification for GFG Practice: return vector{l+1,r}; instead of return vector{l,r-1};
@chrisogonas3 жыл бұрын
Very well explained! Thanks
@siddharthashankar83962 жыл бұрын
Very Helpful
@vithleshkumargupta4 жыл бұрын
you are doing great work👍👍
@techdose4u4 жыл бұрын
Thanks :)
@karanalang15734 жыл бұрын
nice explanation .. can you pls provide sample code ?
@techdose4u4 жыл бұрын
Will try to.
@gopikamurali4404 жыл бұрын
Pls give code
@selectivitism4 жыл бұрын
This solution is so damn freaking clever!
@techdose4u4 жыл бұрын
:)
@ecstacist67493 жыл бұрын
Thank you ! What if the solution elements are non_consequtive
@PankajSingh-mm3br3 жыл бұрын
A subarray is a contiguous part of array. An array that is inside another array
@cuteminired65508 ай бұрын
@@PankajSingh-mm3br thanks
@Usurperhk4 жыл бұрын
What if the left pointer become equal to or exceeds the right pointer??? FIX IT!!!!
@TechDextro3 жыл бұрын
what if there are negative elements in the array
@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_ai25 күн бұрын
subarray by definition means contiguous ,, i guess that should be in the description
@howardlam618125 күн бұрын
@@Spark_T_ai yea it's actually subsequence in that case. It was for a different problem but I figured out the solution anyway.
@c56tejashsharan379 ай бұрын
it will not work if there will be negative numbers in the subarray
@kashifanwar97334 жыл бұрын
nice approach
@techdose4u4 жыл бұрын
:)
@varun84414 жыл бұрын
what will be termination condition if we don't find and sum equal to given sum??
@techdose4u4 жыл бұрын
Termination will be when right pointer goes out of bounds, that is, when right pointer goes beyond last value in array.
@eeshgadol2 ай бұрын
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
@gauravborkar13453 жыл бұрын
Excellent
@vivekkumar-fk9lc4 жыл бұрын
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
@hersheynohara58874 жыл бұрын
Thanks for the video...
@techdose4u4 жыл бұрын
Welcome :)
@chetanpadhen7780 Жыл бұрын
Sir If You Also Go Through To The Code,It Will Be More Helpful. Thanks
@techworld30432 жыл бұрын
what software do you use to teach ?
@this.supratim4 жыл бұрын
Great explanation 👌
@techdose4u4 жыл бұрын
Thanks :)
@inderjeetchawla5274 жыл бұрын
Bhai negative elements bhi ho array me to. And Bhai jab video banao to saare cases cover Kia Karo plz.
@techdose4u4 жыл бұрын
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.
@ayushkashyap82994 жыл бұрын
I think finding all subsets of a array is O(2^n) not O(n^2). Correct me if I am wrong.
@techdose4u4 жыл бұрын
There are 2^n possible subsets of an array. So yeah, you are correct. I might have mistakenly told something, my bad.
@indyarockers4 жыл бұрын
all subsets of array is O(2^n) but consecutive subsets of an array is just O(n^2).
@secularph84244 жыл бұрын
That's for non continuous Subarray, For continuous Subarray its O(n^2)
@AvinashJ213 жыл бұрын
To move pointers we need while loop ?
@mameabera58522 жыл бұрын
what if the array is [17, 85, 93, -45, -21] and sum = 150 . this doesn't work by your algo
@varun84414 жыл бұрын
what will be termination condition if we dont find and sum equal to given sum??
@srajsekhar40614 жыл бұрын
Nice explanation 👌👌👌 bro
@techdose4u4 жыл бұрын
Thanks bro :)
@Nikita-hv5jm3 жыл бұрын
Can we use kdane's algo for this problem?
@techdose4u3 жыл бұрын
Try 2 pointer technique.
@shashikumari40003 жыл бұрын
What if sum is 9 only 5 and 4 satisfies.... Can someone explain this🙏 how will the algorithm with
@mandeepdas3513 жыл бұрын
the explanation is about finding subarray and not subsequence. 5 ,4 is not a subarray.
@nabeelahahmed46103 жыл бұрын
Can u share the entire code plz? That will be better to understand more properly
@akshayzade97613 жыл бұрын
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
@aryankhandelwal153 жыл бұрын
o bhai bhot hard lg rha tha
@tarzanyt30474 ай бұрын
you are amazing
@leetcoder65692 жыл бұрын
Great
@shrimatkapoor22004 жыл бұрын
Also, this would only work for continuous sub-arrays would it not?
@techdose4u4 жыл бұрын
Subarray is continuous only. If something is not continuous then it is either subsequence or subset. This will work only for subarrays.
@shrimatkapoor22004 жыл бұрын
@@techdose4u Cool I think I was looking for the solution for subsets and target sums. Thanks for the clarification.
@snehill72752 жыл бұрын
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_173 жыл бұрын
CAN WE USE THE SAME METHOD IF THERE ARE MORE THAN ONE SUB-ARRAY WITH SUM =K ? and is it sliding window ?
@parthanuj8611 Жыл бұрын
I think that would not be the case when u are solving porgram in leetcode or gfg but yeah it is possible ..
@koushikshomchoudhury91084 жыл бұрын
What you've shown is called a Sliding Window approach. Unfortunately it's an O(n^2) solution.
@techdose4u4 жыл бұрын
You are mistaken. It's O(N) only. How did you conclude O(N^2) ?
@sonusangwan78985 жыл бұрын
great video brother
@techdose4u4 жыл бұрын
Thanks :)
@leetcodepythonsolutionsand93964 жыл бұрын
Can you provide explanation of the DP solution?
@surajsingh-sm7qx4 жыл бұрын
U r awesome
@poonamsangale40373 жыл бұрын
Sir you have explained very well but can you please provide us code
@techdose4u3 жыл бұрын
I was not providing the code earlier but now I do.
@shouryagupta87633 жыл бұрын
nice sir
@dev.suyash3 жыл бұрын
Sir,Can you please share code for this mentioned approach?
@akshayzade97613 жыл бұрын
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; }
@vinayak186f33 жыл бұрын
Will it work for array with some negative elements ? I think it won't , correct me if wrong
@sigma72533 жыл бұрын
what if we start arr[0] is -ve
@shrimatkapoor22004 жыл бұрын
Isn't the number of subsets of a finite set 2^n for n cardinality of the set
@techdose4u4 жыл бұрын
You are correct. But here we are solving subarray which is continuous and not subset. So it will be N^2 for subarray.
@rahu11195 жыл бұрын
which presentation software is this ?
@khushijain35743 жыл бұрын
which software you are using
@ajaygonepuri2285 Жыл бұрын
thank you sir!
@atiquesiddiqui7104 жыл бұрын
Can you please provide the code for this
@euphoric34643 жыл бұрын
I don't think this will work for all kind of arrays like a={2,4,6,8,9,10}
@tokirmanva22473 жыл бұрын
This approach won't work if there are negative numbers also.
@comradepb2 жыл бұрын
Two pointer approach is not good here because the array can have negative numbers.
@sukdipkar88764 жыл бұрын
yaa it is going well but it exceeded time limit.
@ryan-bo2xi4 жыл бұрын
this uses dynamic window technique
@techdose4u4 жыл бұрын
Yea :)
@K_EC_AushoRoup4 жыл бұрын
Agreed
@akshhay4 жыл бұрын
Ye contigious hogyi, what if we want any subarray?
@theWrongCode4 жыл бұрын
we changed this problem with array containing multiple subarray with given sum.
@prabhatkashyap83334 жыл бұрын
did the same on geeksforgeeks platform, showing TLE.
@techdose4u4 жыл бұрын
Should not give TLE bro :( Is there any algo to calculate below O(N) time?
@prabhatkashyap83334 жыл бұрын
@@techdose4u i think sliding window, thats what i am also looking for.
@techdose4u4 жыл бұрын
@@prabhatkashyap8333 yes correct.
@prabhatkashyap83334 жыл бұрын
@@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?
@steevjames672010 ай бұрын
What if there are negative numbers ?
@yaswanthreddy3724 жыл бұрын
thanks bro,its working
@techdose4u4 жыл бұрын
Welcome :)
@dayagutte454 жыл бұрын
@@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]
@dayagutte454 жыл бұрын
Can u provide a code plz
@dayagutte454 жыл бұрын
Can u provide a code plz
@user-so9jd2nf4s2 жыл бұрын
Hey, how to find multiple sub arrays?
@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 Жыл бұрын
Two pointer approach
@pepetikesavasiddhardha78522 жыл бұрын
simple yet sexy logic....
@piyushverma57974 жыл бұрын
find subarray with given sum negative numbers allowed in constant space
@sunnyvarun66524 жыл бұрын
How will this problem be solved? int arr[]= {1,4,45,6,10,8}, target=11.
@satyamsingh_474 жыл бұрын
This is a subset sum problem which is different from a subarray sum problem
@techie_bhuvi3 жыл бұрын
This will fail for negative number [-1,-1,1] expected 1 but this code will return 0
@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
@neghatnazir16684 жыл бұрын
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
@supratikpadhee42354 жыл бұрын
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 )
@supratikpadhee42354 жыл бұрын
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).
@neghatnazir16684 жыл бұрын
@@supratikpadhee4235 thanks :)
@neghatnazir16684 жыл бұрын
@@supratikpadhee4235 thanks :) i checked it after
@runtimeError1232 жыл бұрын
It will fail bro in many cases Example [-1, -1, 0] and k=0 . Here he have to count entire array also.