Тёмный

Longest Subarray with sum K | Brute - Better - Optimal | Generate Subarrays 

take U forward
Подписаться 623 тыс.
Просмотров 474 тыс.
50% 1

Notes/C++/Java/Python codes:
Longest Subarray with sum K [positives]: takeuforward.org/data-structu...
Longest Subarray with sum K [positives + negatives]: takeuforward.org/arrays/longe...
Problem links.
Longest Subarray with sum K [positives]: bit.ly/3GHyBOS
Longest Subarray with sum K [positives + negatives]: bit.ly/3mNSZ9u
We have solved the above problems, and we have gone from brute force and ended with the most optimal solution.
Full Course: bit.ly/tufA2ZYt
You can follow me across social media, all my handles are below:
Linkedin/Instagram/Telegram: linktr.ee/takeUforward
0:00 Introduction of course

Опубликовано:

 

12 мар 2023

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 643   
@takeUforward
@takeUforward Год назад
Let's march ahead, and create an unmatchable DSA course! ❤ Timestamps pleaseeee Use the problem links in the description.
@vishal_s583
@vishal_s583 Год назад
Thanks for your effort ❤️
@factsmadeiteasy9943
@factsmadeiteasy9943 Год назад
Bhaiya how can I keep that check marks permanently... because it always get cleaned up 😩
@leoved1073
@leoved1073 Год назад
@@factsmadeiteasy9943 bro history ya cookies clear mat karo tumhari progress cookies me save hoti hai...
@factsmadeiteasy9943
@factsmadeiteasy9943 Год назад
@@leoved1073 oh thanks bro 👍🏻
@111rhishishranjan2
@111rhishishranjan2 11 месяцев назад
i think that this problem could be done by just using hashmap.
@ashutoshbhardwaj1369
@ashutoshbhardwaj1369 Месяц назад
I've been watching this lecture for the last two days, and it was so frustrating that I couldn't understand the concept, even though Raj Bhaiya explained it thoroughly. After putting in about 7-8 hours, I finally understood what was holding me back. Here's what I've learned: 1. map_name.find(value)-> This function returns an iterator to the element with the key `value` if it is present in the map. If the key is not found, it returns an iterator to `map_name.end()`. 2. map_name.end()-> This function returns an iterator one past the last element of the map. If your map has a size of 4 (indexing from 0 to 3), `map_name.end()` will give you an iterator representing index 4, which is beyond the last element. 3. if(map.find(value) != map.end())-> This line was a real pain in my ass so basically this line checks whether the key `value` is present in the map. If `map.find(value)` does not equal `map.end()`, it means the key is found in the map.similarly , if `map.find(value)` equals `map.end()`, the key is not present in the map. Understanding this line was crucial for me. The `.find()` function returns an iterator to the element if it is present, or `map.end()` if it is not. So, the condition `if(map.find(value) != map.end())` essentially means, "if the key `value` is found in the map." If you've read this far, all the best, buddy! I was literally crying inside after watching the video again and again, but if a noob like me can understand it, you will definitely nail it. Haha. Thanks, Raj Bhaiya. ❤
@Srijan-ye7xw
@Srijan-ye7xw Месяц назад
Proud of your efforts bud ❤ let's keep going
@archanashah4542
@archanashah4542 Месяц назад
Thanku this helped a lot
@randomtsareenalover9014
@randomtsareenalover9014 Месяц назад
thanks a lot bro, im on the hard section rn and was stuck on the longest zero sum problem and i found this!
@dikshabisen504
@dikshabisen504 Месяц назад
you stuck over the thing where map comes....i cant even get the approach ...ye longest subarray ka approach hi nhi ghus rha bhaiii
@nirbhaybhardwaj4330
@nirbhaybhardwaj4330 28 дней назад
Thanks a lot buddy!
@viveknandan4950
@viveknandan4950 Год назад
When Striver says no one will teach you in such depth, he means it
@Awanderer15
@Awanderer15 Год назад
To all those whose mind are like " shyd coding is not for me" after seeing this question Take my one advice am the one solving it from 11 :am in the morning and understood it at 7 20 pm of evening I between my mind qas like chod de "pr bhaiya chodna hi toh nhi hai tabhi toh smjega" So give yourself a shootout and say striver bhaiya i will play you once more and play it back to back even if you have watched for 100 times play it slowly took a pen copy and try to understand what bhaiya is saying 🎉🎉
@thevaishndra
@thevaishndra 5 месяцев назад
was literally feeling the same, but you sowed a new hope in me
@vartikasharma2587
@vartikasharma2587 4 месяца назад
thanks for this comment !!!instilled hope in me.
@rishabhinc2936
@rishabhinc2936 3 месяца назад
cringe
@v.keerthigakeerthi428
@v.keerthigakeerthi428 3 месяца назад
🥺🥺
@DTALKS01
@DTALKS01 3 месяца назад
i am abouve to change a someone else video but you give me hop
@JustExploreByTravel
@JustExploreByTravel Месяц назад
I am following your playlist for DSA. I got subarray of string related problem in my JP Morgan interview. I have explained the approach as you taught. It was quite simple for me to explain the approach as I had watched your this video.. Thank you so much sir you are doing very well.
@Wonderfully_ok
@Wonderfully_ok 2 дня назад
did u get into jp morgan?
@Piyushraj0
@Piyushraj0 Год назад
TIMESTAMPS 0:00 Intro 0:43 Problem Explanation 3:37 Brute force approach 10:50 Better approach (Both positives + negatives) 26:57 Time complexity of Hashing based better solution and why its best for both positives and negatives 28:55 Optimal solution (positives only) 37:52 Time complexity of two pointer based optimal solution
@MeiteiProgrammer
@MeiteiProgrammer Год назад
thanks dude
@Yseja
@Yseja Год назад
Thank you bro
@salihedneer8975
@salihedneer8975 10 месяцев назад
Striver please add this so everyone wud be get useful of this
@LeelaLakshmiKundi
@LeelaLakshmiKundi 9 месяцев назад
thanks bhai
@gunslingerlynx3964
@gunslingerlynx3964 Месяц назад
That hashing one went above my mind maybe need to watch it again
@utsavseth6573
@utsavseth6573 Год назад
Please keep this series continued. Brilliant work Raj. I am speechless. An if anyone reading this, and you have a difficulty understanding, don't give up, just watch 2-3 more times, you shall understand, trust me.
@Akash-yr2if
@Akash-yr2if Год назад
LOL, I came back today and now the things are getting cleared.
@shivvratraghuvanshi522
@shivvratraghuvanshi522 11 месяцев назад
​@@Akash-yr2ifyeah!!
@barnam_das
@barnam_das 6 месяцев назад
thats the spirits boys, lets goooooo !!!!!!!!
@sourabhtiwari5137
@sourabhtiwari5137 4 месяца назад
for one question , 3 hours is it a joke
@DK-ox7ze
@DK-ox7ze Год назад
Absolutely brilliant. Haven't seen anyone else on RU-vid explaining any dsa problem with this much clarity.
@tanya8353
@tanya8353 8 месяцев назад
You are doing a wonderful job man!!! Really grateful to people like you who provide such quality content with such a dedication !!
@afzhalahmed2051
@afzhalahmed2051 Год назад
You are helping families not individuals. Thanks for this.
@vm1662
@vm1662 9 месяцев назад
Teaching is an art and you have nailed it, my friend. Another amazing one from Striver. Thank you!
@eklavya22k34
@eklavya22k34 Год назад
Those who understand the beauty of ur optimal codes, will agree, ALL UR LEC.S ARE PURE GOLD. U are a gr8 GURU .... sharing such exceptional lec.s for free is superb SERVICE FOR MANKIND. U are empowering youth ... making difficult topics look like easy .... u explain everything so easilty ... study looks like fun when u teach. Codes can revolutionise the world ... can take us to distant planets.... Codes are like miracle ... the way incredible creation like AI is developed .... Sw Er.s are next Gen soldiers of nation. They can fuel engine of growth and contribute in development of nation and growth of humanity. U r doing exceptional work ... Thank for evth..... God bless u ... Tc.
@tahaansari5621
@tahaansari5621 Год назад
Had to watch it twice to understand it like 95% but did understand it. Really impressive stuff!
@takeUforward
@takeUforward Год назад
It's easy if you do a dry run on yourself
@DevashishJose
@DevashishJose Год назад
@@takeUforward yes I was also confused but when I did the Dry run I got it.
@crazybro4383
@crazybro4383 4 месяца назад
Mujhe both positive and negative ka optimal samajh hi ni aaya, like approach I understood in one go and found it very easy. But code ni smjh aaya 2 baar dekhne par bhi. I also refered to striver's website but smjh hi ni aara kuch. Should I leave it for now ? (ps- I am a beginner first year student)
@ViditPandey06
@ViditPandey06 4 месяца назад
@@crazybro4383 just keep working you will eventually understand.
@cinime
@cinime Год назад
Understood! Amazing explanation as always, thank you very very much for your effort!!
@soumiyamuthuraj3516
@soumiyamuthuraj3516 Месяц назад
Amazing quality, Cant thank you enough. top notch explanation, and nobody teaches DSA in this depth from my learning experience. i dont have words to thank you, may god give you all good health , happiness and wealth to continue this noble cause,
@joeljacob4685
@joeljacob4685 11 месяцев назад
Absolutely Brilliant Explanation !! You explained the problem with so much clarity.
@AbhishekPandey-dj2eo
@AbhishekPandey-dj2eo Год назад
Thank you striver for this ultimate course, I was trying from many days and searching to starts DSA, and this course made me do it Today day 15 😅
@akworld2739
@akworld2739 9 месяцев назад
hello bhai apki growth kaisi hai abhi kya question ban rhe mai beigner hu mujse to easy bhi nhi ho rhe hai kitna time spend karna chiye ek question mai
@renewbhagwat5475
@renewbhagwat5475 Месяц назад
@@akworld2739 same goes for me . ab ban rahe hai kya bhai questions ?
@tanishqbansal8462
@tanishqbansal8462 Год назад
Understood !! Both Interviewer And Me Are Super - Duper Impressed !!
@vigneshpandi3013
@vigneshpandi3013 4 дня назад
This man is a god straight from heaven for programmers. The Clarity he has in his explanation is absolutely Amazing! Never seen a guy who is so passionate teaching DSA. Love you Raj Bhai! ❤
@vinaynagle720
@vinaynagle720 11 месяцев назад
This is the lecture when I understand hashing properly. Thanks striver👍
@arunavabanerjee5608
@arunavabanerjee5608 Год назад
The best video till now I have seen for this problem . 41 minutes explanation for a single problem is just a next level thing.
@juliechoudhary9582
@juliechoudhary9582 Год назад
Thank you so much for such an easily understandable course.
@Srinivasssssss
@Srinivasssssss 7 месяцев назад
Outstanding Explaination... I cant able to understand by watching this video even 2 times. It doesnt mean, striver didn't explained well. He explained very well. so, people who are not able to understand it well, try to watch it again and again...! Dont search for other videos for this question, and if you even search, I will assure you that, you cant able to find better explaination than this..! Striver, Thank you so much...!
@user-ge4il7kv1n
@user-ge4il7kv1n Месяц назад
One of the best explanation for this type problem. Need to see the video 3 times but at the end got it.
@simran5048
@simran5048 11 месяцев назад
That much clarity and explanation 🔥🔥🔥
@user-fc3rj6wl9l
@user-fc3rj6wl9l Год назад
This lecture is too good 😍🤩 and the Mentor , he obviously needs no introduction 💥
@code247
@code247 7 месяцев назад
wonderful you've taught both approaches together.
@Atharva_kathiyawadi
@Atharva_kathiyawadi Год назад
kya aadmi h yaar!! dedication amazing 😍
@Akash-yr2if
@Akash-yr2if Год назад
Watching the video 2nd times, first time was yesterday and now things are getting little inside my head. Thnx Striver.
@hritikshah54
@hritikshah54 5 месяцев назад
Amazing!!💗💗 Also, we can further trim down the code provided in 26:46. If we intialize our HashMap with map.put(0,-1), we can skip checking the condition of if(sum==k)
@itzmartin20
@itzmartin20 11 месяцев назад
what a crystal clear explaination, understood!
@AyuKG
@AyuKG Год назад
thank you so much striver for everything ,today i did this question on my own without watching video but only with my intution it took me around 3hrs but it felt so good and its all because of you. thanks a lot (raj sir)😇❤ @take U forward !!🔥🔥🔥🔥🔥🔥🔥
@abhaysingh4-yearb.tech.mec565
@abhaysingh4-yearb.tech.mec565 5 месяцев назад
Bhai kha placement hui
@ishangujarathi10
@ishangujarathi10 Год назад
Amazing intuition as always!!!!
@md.ualiurrahmanrahat2400
@md.ualiurrahmanrahat2400 11 месяцев назад
This is life saving. Salute!
@rohansachdeva627
@rohansachdeva627 7 месяцев назад
Took me 3-4 passes to finally get the thought process through my head. But UNDERSTOOD it finally :)
@footballcreativeeverywhere260
You are my inspiration bhaiya .. And I am really a big fan of you, blessing me to become like you.
@visheshdwivedi2256
@visheshdwivedi2256 3 месяца назад
Making my DSA journey superb ❤ hats off to u
@charan123rams3
@charan123rams3 Год назад
Thank you so much sir 🙏your keep free time to teaching for us
@transformers-ai
@transformers-ai Месяц назад
this series is really commendable. Learning a lot.
@dhruvrawatt9
@dhruvrawatt9 Год назад
following and loving this course ❣❣
@wanderer_ankur
@wanderer_ankur 10 месяцев назад
awesome explanation, i will watch it again and then solve to understand it completely.
@naveensaicremsiyadlapalli3769
excellent explanations as expected.
@user-hn3pf5pb3n
@user-hn3pf5pb3n 4 месяца назад
genuinely one of the best cousrse man!!
@tanmaykarn
@tanmaykarn Год назад
Understood in one go. but had to pause a lot and dry run everything in pen paper. nice question!
@kushagramishra5638
@kushagramishra5638 Год назад
understood ! Next level explanation sir 🙇‍♂
@codingaspirant9446
@codingaspirant9446 Год назад
You are like Virat Kholi.
@justcode7326
@justcode7326 Год назад
Consistent and hardworking
@vinaykrishna747
@vinaykrishna747 Год назад
Yes...
@arnab027
@arnab027 Год назад
Better than kohli kohli can't secure 1000 students' future but striver can.
@helloworld2054
@helloworld2054 Год назад
Don't compare a great teacher with a choker
@calisthenics5247
@calisthenics5247 Год назад
​@@arnab027 yeah but kohli do donate money to save lives of lakhs of people and he also made our country pride a large number of times you fools
@prathameshborse258
@prathameshborse258 2 месяца назад
Understood Bhaiya ❤❤ crystal clear and to be point explanation..!
@knl-tu1no
@knl-tu1no Год назад
Awesome sir.Kindly solve String problems as soon as faster. It will be helpful for my placements.
@PrithaMajumder
@PrithaMajumder 2 месяца назад
Raj, Thanks a lot for This Amazing Video about C++ Arrays Video - 4 Completed ✅
@sayakghosh5104
@sayakghosh5104 Год назад
Understood super explanation as always.
@akashddeepchitransh4537
@akashddeepchitransh4537 10 месяцев назад
Really power packed video 🥲
@domorebemore3192
@domorebemore3192 7 месяцев назад
That satisfying song at the end .I always wait till the end for that.😂😊❤
@user-le3pr5mr1j
@user-le3pr5mr1j Год назад
One the most Amazing Problem Solving Teaching Technique stepwise thoughtfully!! 🤩Seen ever anywhere!! Great Thanks! Striver😊
@hiranmayeekasinadhuni8807
@hiranmayeekasinadhuni8807 Год назад
In the brute force(only +ve) can i break from the jth loop if the sum > target. Since this is not a subsequence i felt there is no point on adding to the sum if its > target.. Correct me if i am wrong
@shatulbansal4756
@shatulbansal4756 6 месяцев назад
Heyy Striver, Can we use the variable-sized sliding window approach for this question ??
@AdityaKumar-be7hx
@AdityaKumar-be7hx Год назад
WE can simplify moving to right is we just initilize the sum to zero: int longestSubarrayWithSumK(vector nums, long long targetSum) { int n = nums.size(); // Size of the array. int left = 0, right = 0; // Two pointers. long long currentSum = 0; // Initialize currentSum to 0. int maxLength = 0; while (right < n) { currentSum += nums[right]; // Add the current element to the currentSum. // If the current sum is greater than the target sum, reduce the subarray from the left // until the current sum becomes less than the target sum. while (left targetSum) { currentSum -= nums[left]; left++; } // If the current sum is equal to the target sum, update the maxLength. if (currentSum == targetSum) { maxLength = max(maxLength, right - left + 1); } // Move the right pointer forward. right++; } return maxLength; }
@himadrimahata8668
@himadrimahata8668 Год назад
thanks a lot
@IncredibleJourneyIndianRailway
@IncredibleJourneyIndianRailway 5 месяцев назад
thank you
@tunepa4418
@tunepa4418 2 месяца назад
Exactly, prefix sum is not necessary here. I am assuming he is just trying to explain the concept but it's really not necessary as all the numbers are positives
@chinmaykonchada
@chinmaykonchada 7 месяцев назад
thank you sir!! for u r wonder lecture and i understood every conceotsir
@purvi9958
@purvi9958 Год назад
Thankyou sir for such an elaborate explanation!
@musicalcasio5146
@musicalcasio5146 10 месяцев назад
in which year you are...?
@ANMOLAHIRWAR-kg8lv
@ANMOLAHIRWAR-kg8lv 15 дней назад
a wonderful question and a wonderful explaination.
@krishnasharma-le1mc
@krishnasharma-le1mc Год назад
This is the Bestttttt Videooo I had found on RU-vid
@luckydev9006
@luckydev9006 10 месяцев назад
Finally understood the concept 😮‍💨
@amanshrivastav0405
@amanshrivastav0405 6 месяцев назад
In the first question, can we break the inner loop if the sum is greater than the required k because the loop only contains positive integers? int longestSubarray(vectorarr, int k){ int maxLength = 0; for(int i = 0; i < arr.size(); i++){ int sum = 0; for(int j = i; j < arr.size(); j++){ sum += arr[j]; if(sum == k){ maxLength = max(maxLength, j-i+1); }else if(sum > k){ break; } } }return maxLength; }
@vishal_s583
@vishal_s583 Год назад
Great work ❤️
@shaktirajable
@shaktirajable Год назад
can someone explain me the part where we update the map by -- preSumMap[sum] = i;
@artofwrick
@artofwrick Год назад
Can't we do it with if else within one while loop in two pointer methods? And number of subarrays: can't it be done with counter?
@Dreamer_07
@Dreamer_07 Год назад
Thanks for the video!
@Ramsiya658
@Ramsiya658 Год назад
Understood much love to you striver
@Lucifer-xt7un
@Lucifer-xt7un Год назад
This is called consistency❤❤
@aashishas7044
@aashishas7044 10 месяцев назад
nice Lecture. understood clearly bro...
@prasannagodiyal6736
@prasannagodiyal6736 9 дней назад
bahut time lag gaya samajhne mai but finally samajh aa hi gaya
@AkashYadav-md1xf
@AkashYadav-md1xf 7 месяцев назад
understood bhaiya i love this videos and sheet loving the content quality and explanations 🤩😍😇💯
@konankikeerthi
@konankikeerthi Месяц назад
Understood. Thanks for your efforts bro.
@yamini436
@yamini436 Год назад
Love u striver bhaiya. You are DOPE!
@ChandanMahara-we4dj
@ChandanMahara-we4dj 2 дня назад
Thank you so much it helped a lot 🙏
@user-ip8ui4tm9x
@user-ip8ui4tm9x 2 месяца назад
Understood great video for arrays problem solving
@darshantawte7435
@darshantawte7435 Год назад
25:24 I understood what you told but C++ map by defaults takes the right value i.e for input [2 0 0 3] i tried printing (prefixsum,index) hashmap at it printed (5,3) and (2,0) so i guess it never got updated after first insertion.
@_hulk748
@_hulk748 Год назад
Understood sir thankyou sir🙏🙇‍♂️❤
@subratkumarsahoo3785
@subratkumarsahoo3785 10 месяцев назад
18:17 I can't understood the brute solution using hashing. It goes beyond my head😂..
@suyashagrawal1102
@suyashagrawal1102 4 месяца назад
bruh try rewatchung the video
@manavmalhotra5024
@manavmalhotra5024 3 месяца назад
true i figure out the optimial solution before brute and better approach
@CodeWithMeee
@CodeWithMeee 2 месяца назад
same, and also hashing means use of hashMap right but where he used Hashmap ? please someone correct me if I am wrong
@nirbhaybhardwaj4330
@nirbhaybhardwaj4330 26 дней назад
@@CodeWithMeee bro! map is also used for hashing .
@CodeWithMeee
@CodeWithMeee 8 дней назад
@@nirbhaybhardwaj4330 yes, now i understand...I have no knowledge about that earlier. thanks
@itskaaryan7
@itskaaryan7 10 месяцев назад
at 28:28 he talks about collisons in case of using unordered_map , what does it mean, can anyone explain me?
@Sillysmiles76
@Sillysmiles76 Год назад
You made DSA very interesting😄
@dhruvsolanki4473
@dhruvsolanki4473 Год назад
Super cool explanation.
@abhishekgarg5017
@abhishekgarg5017 5 месяцев назад
best In-depth explanation
@mahendar7733
@mahendar7733 10 месяцев назад
understood Great concepts
@girisathvikavpragatiengine309
@girisathvikavpragatiengine309 25 дней назад
can someone explain how ordered and unordered maps are effecting the TC.
@priyadarsinipaikaray6271
@priyadarsinipaikaray6271 Год назад
dil kush kardittaa sir ji ❣
@aniketpanchal206
@aniketpanchal206 Год назад
can you add real world applications based on such questions, or approaches.
@chethanprabhu4475
@chethanprabhu4475 Месяц назад
@Striver JUST FYI. The problem link attached in the striver A to Z sheet is of total count of sub arrays and not longest subarray
@dinimeegada9167
@dinimeegada9167 Год назад
Big fan of your teaching and effort anna ❤
@blue.262
@blue.262 Год назад
Anna Are you telugu??
@AS-gf3ci
@AS-gf3ci Год назад
It's a bit complex to learn this in the first go. Maybe it would require re-watching the same multiple times.
@rohanmajhi1329
@rohanmajhi1329 Год назад
Yeah, I thought that was not my cup of tea but after watching it 2nd time, I got the approach!!
@S3aw0lf
@S3aw0lf 11 месяцев назад
Watched it 3 times now to toally understand tho it's still a bit unclear but the explanation was really good hope I'll understand if I watch once more
@anmol3749
@anmol3749 6 дней назад
Understood Thank you Striver
@rinokamura1152
@rinokamura1152 5 месяцев назад
But when did we store the sum of the array elements in the map .?? Please anyone help me , how do you store the sum of the elements of array into the map with their indexes?
@user-lq2ld6cg6v
@user-lq2ld6cg6v 5 месяцев назад
Would it make a difference in the approach if for the optimal solution we initialize the sum to 0 , rather than initializing it to sum = a[0];
@LearnwithEase20
@LearnwithEase20 9 месяцев назад
Great explanation
@DevashishJose
@DevashishJose Год назад
understood. thank you.
@raghumanda2tanush496
@raghumanda2tanush496 Год назад
brilliant 2 pointer explanation
@guneeshvats46
@guneeshvats46 Месяц назад
Amazing explanation striver
@vamshi2639
@vamshi2639 2 месяца назад
That OVERALL was crazy !😂😂
@vibhu1768
@vibhu1768 10 месяцев назад
why is my exact same code of 2 pointer approach passing just 3 test cases?? can someone suggest the reason please!
Далее
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
Coding Interviews Be Like
5:31
Просмотров 6 млн
5 Math Skills Every Programmer Needs
9:08
Просмотров 1 млн
How I would learn Leetcode if I could start over
18:03
Просмотров 376 тыс.
Please Master These 10 Python Functions…
22:17
Просмотров 101 тыс.