Kadane's Algorithm - DSA with Javascript | Maximum Subarray Sum | Frontend DSA Interview Questions

  Рет қаралды 22,976

RoadsideCoder

RoadsideCoder

Күн бұрын

Пікірлер
@RoadsideCoder
@RoadsideCoder Жыл бұрын
➡ Download my DSA with JS E-Book - topmate.io/roadsidecoder/491565 ✅ Follow me on Instagram to be updated about next videos - instagram.com/roadsidecoder/ If this video gets good response, I will make more DSA videos, so do share it with others 🔥
@DhruvSharma3
@DhruvSharma3 25 күн бұрын
This is one of the most simplified and intuitive explanation for kadane's algorithm and logic behind the working of SUBARRAY SUM in general. Kudos !!! ///////////////////////////////////////////////////////////////////// ///// Cleanest way to return subarray as well ///// ///////////////////////////////////////////////////////////////////// const array = [0,0,0,0,0,1,40,-40,200]; (function (array) { let sum = 0, maxSum = 0, start = 0, tempStart = 0, end = 0; array.forEach((element, index) => { sum += element; if (sum > maxSum) { maxSum = sum; start = tempStart; end = index; } if (sum < 0) { sum = 0; tempStart = index + 1; } }); console.log({"Max Sum": maxSum, "Subarray": array.slice(start, end + 1)}); })(array); Happy coding floks !!
@DamnSerious12
@DamnSerious12 Ай бұрын
I submitted this solution, and it beats 100% in time complexity. You are way good bro.
@PriyaNayak-xo9yg
@PriyaNayak-xo9yg Жыл бұрын
Sir please keep it in you tube there is few teachers who teach DSA in javascript in advance level you going very well explaination technique is fabulous sir 👏
@eleah2665
@eleah2665 Жыл бұрын
Very good. The word "contiguous" works here also.
@jayeshcholker
@jayeshcholker 9 ай бұрын
Awesome man valuable content 💯❤
@prashanthgv86
@prashanthgv86 10 ай бұрын
Only by your video, I was able to understand the question or this algorithm.. You explain things so well on all your videos! Amazing you guys are.
@RoadsideCoder
@RoadsideCoder 10 ай бұрын
Awesome, thank you!
@kaushikmondal3873
@kaushikmondal3873 Ай бұрын
function maxSubArraySum(arr) { // Edge case: If array is empty if (arr.length === 0) return 0; // Initialize variables let maxEndingHere = arr[0]; let maxSoFar = arr[0]; // Iterate through the array starting from index 1 for (let i = 1; i < arr.length; i++) { // Extend the current subarray or start a new one maxEndingHere = Math.max(arr[i], maxEndingHere + arr[i]); // Update the global maximum maxSoFar = Math.max(maxSoFar, maxEndingHere); } return maxSoFar; } let arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; console.log(maxSubArraySum(arr)); // Output: 6
@crazyvaleideas
@crazyvaleideas Жыл бұрын
Amazing explanation, I never thought Kadane's algorithm was that easy. 😂
@BharathKumar-iq7ku
@BharathKumar-iq7ku Жыл бұрын
Thanks piyush keep up your good work.. Love from netherland❤
@PG-418
@PG-418 9 ай бұрын
Thankyou soo much for your superb explanation @RoadsideCoder👌❤.. Please check below code and correct me if it is wrong.. Get subarray with the help of Kadane's Algorithm: - function getMaxSubArray(nums: number[]) { let maxSum = nums[0]; let sum = 0, min = 0, max = 0, currMin = 0; for (let i = 0; i < nums.length; i++) { sum += nums[i]; if (sum > maxSum) { maxSum = sum; max = i; min = currMin; } if (sum < 0) { sum = 0; currMin = i + 1; } } return { subArray: nums.slice(min, max + 1), maxSum }; }
@vinothmohan4247
@vinothmohan4247 4 ай бұрын
awesome bro
@devlopersessential4349
@devlopersessential4349 Жыл бұрын
Super Duper Video👌👌👌👌
@fallen_turbo
@fallen_turbo Жыл бұрын
I loved the explaination of kidney algorithm by algo agarwal
@RoadsideCoder
@RoadsideCoder Жыл бұрын
😂
@snehil3209
@snehil3209 Жыл бұрын
Hey piyush plz also cover the leetcode 30 day js challenge. Live your work bro❤
@motivationalmusic3721
@motivationalmusic3721 11 ай бұрын
Helpful please complete the series
@rahuls22
@rahuls22 9 ай бұрын
This is so good!
@A9kit.k
@A9kit.k Жыл бұрын
Please continue this DSA course :)
@surajburman7543
@surajburman7543 11 ай бұрын
awesome,need more videos on dsa in js
@RoadsideCoder
@RoadsideCoder 11 ай бұрын
I have made a complete playlist
@surajburman7543
@surajburman7543 11 ай бұрын
@@RoadsideCoder thankyou
@isha25tripathi12
@isha25tripathi12 11 ай бұрын
one question so incase of bruteforce you explained how to get the sub array ,could you please explain how can we get that with the second solution as well?
@victoriakelz2602
@victoriakelz2602 10 ай бұрын
I co-ask
@DjGaming-iy8ml
@DjGaming-iy8ml Жыл бұрын
Please continue this DSA course
@davidhusted817
@davidhusted817 11 ай бұрын
But the issue with this algorithm will be applicable just when we have a positiv number if all numbers less than 0, the sum doesn't updated and return at the end 0 not logique answer
@socialopossum6487
@socialopossum6487 10 ай бұрын
It did work for me with negative numbers as well. The max is always a number from array, and the sum becomes nums[i] before you compare and reset it each round. So the final number will always be someting from the array. E.g. [-5, -4, -1, -7, -8] returns -1
@dipanshuroutray4942
@dipanshuroutray4942 Жыл бұрын
Please make a detailed tutorial on React
@randomsVlogs871
@randomsVlogs871 Жыл бұрын
Love you from Pakistan 🇵🇰❤️
@vaibhavirakeshmore4029
@vaibhavirakeshmore4029 Жыл бұрын
Good❤
@syedabdullahazhar6298
@syedabdullahazhar6298 Жыл бұрын
Thanks man!!!
@shaikmahaboobjani3655
@shaikmahaboobjani3655 Жыл бұрын
please make more videos bro i tried from your explanation Kadane's Algorithm like this let nums = [-2,1,-3,4,-1,2,1,-5,4] let sum = nums[0]; let max = nums[0]; for (let i = 0; i < nums.length; i++) { sum=Math.max(nums[i],sum+nums[i]) max=Math.max(sum,max) } console.log (max);
@ayushjain7023
@ayushjain7023 Жыл бұрын
in for loop i could start from 1 as well
@Darwin2412
@Darwin2412 Жыл бұрын
Hi I am following your DSA in javascript playlist and it amazing ? Just have one dought regarding Kadanes algorithm . Can we get subarray also with the help of this Kadane's Algorithm.
@PG-418
@PG-418 9 ай бұрын
function getMaxSubArray(nums: number[]) { let maxSum = nums[0]; let sum = 0, min = 0, max = 0, currMin = 0; for (let i = 0; i < nums.length; i++) { sum += nums[i]; if (sum > maxSum) { maxSum = sum; max = i; min = currMin; } if (sum < 0) { sum = 0; currMin = i + 1; } } return { subArray: nums.slice(min, max + 1), maxSum }; }
@chiragsharma4102
@chiragsharma4102 Жыл бұрын
What if we have all negative integers and max sum is -10 , how are we going to reject the negative sum in that case?
@Bfhhg-bj3jy
@Bfhhg-bj3jy Жыл бұрын
Just set initial value of max = 0
@ishantkushwaha534
@ishantkushwaha534 8 ай бұрын
The Starting 🤣🤣
@pranjuljain6025
@pranjuljain6025 Жыл бұрын
If currentelement is greater than currentelement+sum than sum = currentelement Done one line code
@RavindraSingh-lp9pl
@RavindraSingh-lp9pl Жыл бұрын
@roadside coder need few more help for below code. Can you please give me solution for below questions asked in React interviews- Question 1. a = [{id: 1, value: 1}, {id: 1, value: 2}, {id: 2, value: 20}, {id: 3, value: 30}] output--> {1: [{id: 1, value: 1}, {id: 1, value: 2}], 2: [{id: 2, value: 20}],3:[{id: 3, value: 30}]} Output should be : a = [{id: 1, value: 1}, {id: 1, value: 2}, {id: 2, value: 20}, {id: 3, value: 30}] output--> {1: [{id: 1, value: 1}, {id: 1, value: 2}], 2: [{id: 2, value: 20}],3:[{id: 3, value: 30}]} *************** Question 2- a = [{sum: 1}, {sum: 2}, {sum: 6}] Output should be sum of all that is 9
@jagadeeshgade6309
@jagadeeshgade6309 Жыл бұрын
Thanks
@prafuldeoghare1904
@prafuldeoghare1904 Жыл бұрын
you didn't explain how to return that subarray
@bhaveshsaxena9861
@bhaveshsaxena9861 Жыл бұрын
It will be great if you also start teach in Hindi
@NageshNani_28
@NageshNani_28 Жыл бұрын
can you update polyfil of all js methodes please
@RoadsideCoder
@RoadsideCoder Жыл бұрын
Already did in my JS interview Playlist
@NageshNani_28
@NageshNani_28 Жыл бұрын
Only map, filter and reduce are there we need more
@RoadsideCoder
@RoadsideCoder Жыл бұрын
Complete Playlist is there bro - kzbin.info/aero/PLKhlp2qtUcSaCVJEt4ogEFs6I41pNnMU5 Watch other videos as well
@NageshNani_28
@NageshNani_28 Жыл бұрын
@@RoadsideCoder all this videos i watched can u make single video for polyfil
@deepakam201
@deepakam201 10 ай бұрын
i liked the explanation. But one thing that is really annoying is when you try to explain the algorithm sometimes you insert that noise video (white and black) which is really kinda irritating. Other than that i like your content
@RoadsideCoder
@RoadsideCoder 10 ай бұрын
Thats just the part of video story that I did, don't worry it was only for limited time.
@purusharma8192
@purusharma8192 Жыл бұрын
Wow
@s0226-n4e
@s0226-n4e Жыл бұрын
Bhai itna easy kaise krlete ho kisi bhi problem/algorithm ko solve😢
@abhishekrajput9291
@abhishekrajput9291 10 ай бұрын
tree and graph ...
@vishwas55
@vishwas55 Жыл бұрын
Bro if possible finish it quickly 😭😭 atleast different algo technique in array and strings
@ProgrammingWithSatyamP
@ProgrammingWithSatyamP 5 ай бұрын
Kidney 😂😂
@MithileshKumar-c1g
@MithileshKumar-c1g Жыл бұрын
Hindi me chahiye video
-5+3은 뭔가요? 📚 #shorts
0:19
5 분 Tricks
Рет қаралды 13 МЛН
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 532 М.
Sliding Window Leetcode Problem Solved with JavaScript
15:01
James Q Quick
Рет қаралды 9 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 684 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 191 М.
-5+3은 뭔가요? 📚 #shorts
0:19
5 분 Tricks
Рет қаралды 13 МЛН