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

  Рет қаралды 488,787

take U forward

take U forward

Жыл бұрын

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

Пікірлер: 652
@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 Жыл бұрын
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 Ай бұрын
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 8 күн бұрын
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
@DK-ox7ze
@DK-ox7ze Жыл бұрын
Absolutely brilliant. Haven't seen anyone else on KZbin explaining any dsa problem with this much clarity.
@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 Жыл бұрын
​@@Akash-yr2ifyeah!!
@barnam_das
@barnam_das 6 ай бұрын
thats the spirits boys, lets goooooo !!!!!!!!
@sourabhtiwari5137
@sourabhtiwari5137 5 ай бұрын
for one question , 3 hours is it a joke
@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 !!
@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 5 ай бұрын
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.
@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.
@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!
@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
@tanishqbansal8462
@tanishqbansal8462 Жыл бұрын
Understood !! Both Interviewer And Me Are Super - Duper Impressed !!
@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,
@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...!
@vigneshpandi3013
@vigneshpandi3013 10 күн бұрын
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 Жыл бұрын
This is the lecture when I understand hashing properly. Thanks striver👍
@cinime
@cinime Жыл бұрын
Understood! Amazing explanation as always, thank you very very much for your effort!!
@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 10 ай бұрын
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 ?
@joeljacob4685
@joeljacob4685 11 ай бұрын
Absolutely Brilliant Explanation !! You explained the problem with so much clarity.
@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.
@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.
@user-fc3rj6wl9l
@user-fc3rj6wl9l Жыл бұрын
This lecture is too good 😍🤩 and the Mentor , he obviously needs no introduction 💥
@simran5048
@simran5048 11 ай бұрын
That much clarity and explanation 🔥🔥🔥
@md.ualiurrahmanrahat2400
@md.ualiurrahmanrahat2400 11 ай бұрын
This is life saving. Salute!
@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.
@ishangujarathi10
@ishangujarathi10 Жыл бұрын
Amazing intuition as always!!!!
@code247
@code247 7 ай бұрын
wonderful you've taught both approaches together.
@juliechoudhary9582
@juliechoudhary9582 Жыл бұрын
Thank you so much for such an easily understandable course.
@visheshdwivedi2256
@visheshdwivedi2256 4 ай бұрын
Making my DSA journey superb ❤ hats off to u
@transformers-ai
@transformers-ai Ай бұрын
this series is really commendable. Learning a lot.
@user-hn3pf5pb3n
@user-hn3pf5pb3n 4 ай бұрын
genuinely one of the best cousrse man!!
@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 6 ай бұрын
Bhai kha placement hui
@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)
@dhruvrawatt9
@dhruvrawatt9 Жыл бұрын
following and loving this course ❣❣
@rohansachdeva627
@rohansachdeva627 7 ай бұрын
Took me 3-4 passes to finally get the thought process through my head. But UNDERSTOOD it finally :)
@wanderer_ankur
@wanderer_ankur 10 ай бұрын
awesome explanation, i will watch it again and then solve to understand it completely.
@PrithaMajumder
@PrithaMajumder 2 ай бұрын
Raj, Thanks a lot for This Amazing Video about C++ Arrays Video - 4 Completed ✅
@footballcreativeeverywhere260
@footballcreativeeverywhere260 Жыл бұрын
You are my inspiration bhaiya .. And I am really a big fan of you, blessing me to become like you.
@naveensaicremsiyadlapalli3769
@naveensaicremsiyadlapalli3769 Жыл бұрын
excellent explanations as expected.
@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 🙇‍♂
@akashddeepchitransh4537
@akashddeepchitransh4537 10 ай бұрын
Really power packed video 🥲
@prathameshborse258
@prathameshborse258 3 ай бұрын
Understood Bhaiya ❤❤ crystal clear and to be point explanation..!
@krishnasharma-le1mc
@krishnasharma-le1mc Жыл бұрын
This is the Bestttttt Videooo I had found on KZbin
@domorebemore3192
@domorebemore3192 8 ай бұрын
That satisfying song at the end .I always wait till the end for that.😂😊❤
@charan123rams3
@charan123rams3 Жыл бұрын
Thank you so much sir 🙏your keep free time to teaching for us
@sayakghosh5104
@sayakghosh5104 Жыл бұрын
Understood super explanation as always.
@luckydev9006
@luckydev9006 10 ай бұрын
Finally understood the concept 😮‍💨
@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 3 ай бұрын
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
@prasannagodiyal6736
@prasannagodiyal6736 16 күн бұрын
bahut time lag gaya samajhne mai but finally samajh aa hi gaya
@knl-tu1no
@knl-tu1no Жыл бұрын
Awesome sir.Kindly solve String problems as soon as faster. It will be helpful for my placements.
@priyadarsinipaikaray6271
@priyadarsinipaikaray6271 Жыл бұрын
dil kush kardittaa sir ji ❣
@ANMOLAHIRWAR-kg8lv
@ANMOLAHIRWAR-kg8lv 21 күн бұрын
a wonderful question and a wonderful explaination.
@Ramsiya658
@Ramsiya658 Жыл бұрын
Understood much love to you striver
@Dreamer_07
@Dreamer_07 Жыл бұрын
Thanks for the video!
@user-le3pr5mr1j
@user-le3pr5mr1j Жыл бұрын
One the most Amazing Problem Solving Teaching Technique stepwise thoughtfully!! 🤩Seen ever anywhere!! Great Thanks! Striver😊
@vishal_s583
@vishal_s583 Жыл бұрын
Great work ❤️
@dhruvsolanki4473
@dhruvsolanki4473 Жыл бұрын
Super cool explanation.
@abhishekgarg5017
@abhishekgarg5017 5 ай бұрын
best In-depth explanation
@ChandanMahara-we4dj
@ChandanMahara-we4dj 8 күн бұрын
Thank you so much it helped a lot 🙏
@konankikeerthi
@konankikeerthi 2 ай бұрын
Understood. Thanks for your efforts bro.
@user-ip8ui4tm9x
@user-ip8ui4tm9x 2 ай бұрын
Understood great video for arrays problem solving
@purvi9958
@purvi9958 Жыл бұрын
Thankyou sir for such an elaborate explanation!
@musicalcasio5146
@musicalcasio5146 10 ай бұрын
in which year you are...?
@chinmaykonchada
@chinmaykonchada 7 ай бұрын
thank you sir!! for u r wonder lecture and i understood every conceotsir
@aashishas7044
@aashishas7044 11 ай бұрын
nice Lecture. understood clearly bro...
@LearnwithEase20
@LearnwithEase20 9 ай бұрын
Great explanation
@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 3 ай бұрын
same, and also hashing means use of hashMap right but where he used Hashmap ? please someone correct me if I am wrong
@nirbhaybhardwaj4330
@nirbhaybhardwaj4330 Ай бұрын
@@CodeWithMeee bro! map is also used for hashing .
@CodeWithMeee
@CodeWithMeee 14 күн бұрын
@@nirbhaybhardwaj4330 yes, now i understand...I have no knowledge about that earlier. thanks
@yamini436
@yamini436 Жыл бұрын
Love u striver bhaiya. You are DOPE!
@Sillysmiles76
@Sillysmiles76 Жыл бұрын
You made DSA very interesting😄
@mahendar7733
@mahendar7733 10 ай бұрын
understood Great concepts
@Lucifer-xt7un
@Lucifer-xt7un Жыл бұрын
This is called consistency❤❤
@anmol3749
@anmol3749 12 күн бұрын
Understood Thank you Striver
@raghumanda2tanush496
@raghumanda2tanush496 Жыл бұрын
brilliant 2 pointer explanation
@torishi82
@torishi82 25 күн бұрын
Samaj aa gaya. Thank you.
@abhijeetmishra3804
@abhijeetmishra3804 Жыл бұрын
superb sir understood
@guneeshvats46
@guneeshvats46 Ай бұрын
Amazing explanation striver
@saouli1632
@saouli1632 7 ай бұрын
37:42 Understood ❤ dada .Thank You
@_hulk748
@_hulk748 Жыл бұрын
Understood sir thankyou sir🙏🙇‍♂️❤
@prodevmahi4901
@prodevmahi4901 Жыл бұрын
Extremety helpful video
@xbenchers1116
@xbenchers1116 Ай бұрын
Best Explanation Ever
@NazeerBashaShaik
@NazeerBashaShaik 4 ай бұрын
Understood, thank you.
@DevashishJose
@DevashishJose Жыл бұрын
understood. thank you.
@shatulbansal4756
@shatulbansal4756 6 ай бұрын
Heyy Striver, Can we use the variable-sized sliding window approach for this question ??
@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!!
@AniketKumar-hf2bo
@AniketKumar-hf2bo 6 ай бұрын
understood properly thnx for the video
@fallenAngel-cw9ki
@fallenAngel-cw9ki Жыл бұрын
understood, ty!!
@ashishphilipthomas
@ashishphilipthomas 4 ай бұрын
Thank you Striver May god bless you
@dinimeegada9167
@dinimeegada9167 Жыл бұрын
Big fan of your teaching and effort anna ❤
@blue.262
@blue.262 Жыл бұрын
Anna Are you telugu??
@prajaktakapoor7520
@prajaktakapoor7520 10 ай бұрын
"UNDERSTOOD" Thanks bhaiya
@AkashYadav-md1xf
@AkashYadav-md1xf 7 ай бұрын
understood bhaiya i love this videos and sheet loving the content quality and explanations 🤩😍😇💯
@riteshbisht94
@riteshbisht94 9 ай бұрын
Virat Kohli of Programming 🌟🗿
@vishalakulkarni2620
@vishalakulkarni2620 9 ай бұрын
Hey did u get how k=3 in 1st eg then in last eg k=6
@aryansinha1818
@aryansinha1818 7 ай бұрын
Thank you so much.
@KarthikDances
@KarthikDances Ай бұрын
Thank you 🎉
@vamshi2639
@vamshi2639 2 ай бұрын
That OVERALL was crazy !😂😂
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 361 М.
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 10 МЛН
Count Subarray sum Equals K | Brute - Better -Optimal
24:09
take U forward
Рет қаралды 253 М.
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 846 М.
Please Master These 10 Python Functions…
22:17
Tech With Tim
Рет қаралды 111 М.
Understanding B-Trees: The Data Structure Behind Modern Databases
12:39
Doing LeetCode Be Like (Coding Interviews Be Like Pt. 2)
4:41
Nicholas T.
Рет қаралды 754 М.