Maximum Sum Circular Subarray (Microsoft, Amazon) : Explanation ➕ Live Coding

  Рет қаралды 9,106

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 101
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Small correction at 3:40 Answer for {5, -3, 5} will be 7
@nagmakhan3165
@nagmakhan3165 Жыл бұрын
Got it
@navinojha5637
@navinojha5637 Жыл бұрын
Why bhaiya it should be 10 only correct ? because if we rotate (5, 5, -3) maximum will be 10 right ?
@navinojha5637
@navinojha5637 Жыл бұрын
sorry got it, I mistaked it with normal subarray sum
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Respect++ Support++ Your videos are addictive
@DeepakKumar-pd3lc
@DeepakKumar-pd3lc Ай бұрын
Brilliant explanation>>>>>>>>>>>>>>>>>
@summisahani6415
@summisahani6415 Ай бұрын
Really impressed with your explanation. Thanks a lot. Keep posting.
@divyangdheer7292
@divyangdheer7292 Жыл бұрын
college walo nai ghanta kuch nhi padhaya kaden's humne khud pdha hai aap jaise youtubers ki help se
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️❤️
@Badalkumar-me9ok
@Badalkumar-me9ok 9 ай бұрын
I have gone to other videos but didn't understand this concept but your video is awesome and now I have no doubt about this, thank you so much bhayia.
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Glad to hear that ❤️❤️😇😇
@wooldan7007
@wooldan7007 Жыл бұрын
Really!! man at 8:00 time stamp my search for actual reason gets over. You are always truly amazing to highlight such important details.👍👍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot to me. Thank you so much 🙏😇
@iamnoob7593
@iamnoob7593 3 ай бұрын
WOW JUST WOW CRAZY GOOD EXPLANATION
@raisanjeeb42
@raisanjeeb42 Жыл бұрын
Tried the same logic for almost 2 hours...everytime wrong answer on some cases...Then Finally video dekha and got the error ..Thanks for breaking it to easier subproblems.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️❤️
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I am so glad Sanjeeb that you tried on your own and gave it time. That’s how we learn. Keep it up
@gui-codes
@gui-codes Ай бұрын
you are just fantastic. deserves more subs 1 M
@shabananoor9423
@shabananoor9423 Жыл бұрын
Unbelievable explanation
@boomburstshorts2347
@boomburstshorts2347 Жыл бұрын
Whoooaaa!!!! Solid Explanation Brother 🔥
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you ❤️😇
@sauravbiswajit8091
@sauravbiswajit8091 Жыл бұрын
Ohhhhh bhai ....great explanation 😮
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️🙏😇
@anmol3749
@anmol3749 7 ай бұрын
Zabardastttt story building!!!!
@arambh-gaur
@arambh-gaur 3 ай бұрын
Great explaination !! Keep at it
@komalkrishna7836
@komalkrishna7836 Жыл бұрын
Wow! Great explanation sir👏 Your explanation is awesome as always, Thank you 😊
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Komal
@yashaggarwal825
@yashaggarwal825 Жыл бұрын
Excellent Sir!! just a small suggestion i would be beneficial to many students if you make videos on contests of leetcode,codechef and codeforces. Its true that your type of content is hard to make but it will influence more students towards better platforms. thank you so much for the videos
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks Yash. I understand your point. A little difficult with so less time but yeah; i will soon be planning to include those you mentioned. Slowly slowly will add more contents
@zaviyakhurshid9097
@zaviyakhurshid9097 Жыл бұрын
Superb, very well explained 🔥
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
So so good bhai..mujhe bhulna ni sb millions subscribers honge apke
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks 😊 Sure 💕
@gaurimandot5788
@gaurimandot5788 10 ай бұрын
As always awesome sir😍
@nagmakhan672
@nagmakhan672 Жыл бұрын
This is Art 🔥🔥🔥
@alokgautam02
@alokgautam02 Жыл бұрын
Thanks for this awesome solution ..😇
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
code of brute force on submitting giving a tle code : class Solution { public: void rotate(vector&a,int n) {int temp=a[0]; for(int i=0;i
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Glad you tried brute force. It’s always a good practice to go for brute force
@rguktiiit371
@rguktiiit371 Жыл бұрын
Bro make a video on time complexities and how to properly read the constraints mentioned in the question
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure. Thanks for the suggestion Added to my list
@HimanshuPatel-wn6en
@HimanshuPatel-wn6en 5 ай бұрын
@@codestorywithMIK Bhaiyia Please make video on how to read constraints and understand that which tc solution will work
@aditimahabole1761
@aditimahabole1761 7 ай бұрын
muje aapke charan chune ....aap itnaa achaa kese padhaaa sakteeeeeeee.
@yashisrivastava6196
@yashisrivastava6196 Жыл бұрын
Nyc explanation 💯
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 🙏😇
@vaibhavgupta2130
@vaibhavgupta2130 Жыл бұрын
nice explanation...thanks
@Ramneet04
@Ramneet04 Жыл бұрын
Hi! were you able to come up with this approach. As even I tried for so long but what did like running kadens algorithm only but having two pointer approach from front and back but it didn't work. As this was a bit biased problem,not so intuitive.? How to like start this approach, infact that rotate array then find maximum makes more sense!!!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi there, Actually i remember when I had solved this problem first time, I couldn’t come up with the exact same approach. I think it’s fine to not being able to come up with such an amazing approach in one go. But once you get this, you can definitely think similar approaches in other similar problems.
@Ramneet04
@Ramneet04 Жыл бұрын
@@codestorywithMIK 🙃 Btw explanation is top notch no doubt 👉👈
@Ramneet04
@Ramneet04 Жыл бұрын
We can also do if (circular sum==0)return maxsum; Else return max(maxsum,circularsum);
@ashishrawat8819
@ashishrawat8819 9 ай бұрын
great video
@thekindspill
@thekindspill 9 күн бұрын
Legend
@danishsinghjamwal627
@danishsinghjamwal627 Жыл бұрын
awesome
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️❤️❤️
@prnvsgr
@prnvsgr Жыл бұрын
respect
@apuljain6625
@apuljain6625 Ай бұрын
🤯
@tarifzaman
@tarifzaman 9 ай бұрын
You are love
@AdityaSingh-ui4tr
@AdityaSingh-ui4tr Жыл бұрын
In [5,-3,5] this case 5+(-3)+5+5 can also be a subarray
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually we can’t take any element twice. The 5 of index 0 is taken twice in “5+(-3)+5+5” which is not valid
@tauquirahmed1879
@tauquirahmed1879 Жыл бұрын
Bhaiya topic tags mein Dynamic Programming aur Monotonic Queue q diya h? Usse bhi koi approach ho skta h kya?
@shanayagupta4002
@shanayagupta4002 Жыл бұрын
haa sir, same question
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes. However i will cover them in separate play lists
@tauquirahmed1879
@tauquirahmed1879 Жыл бұрын
@@codestorywithMIK ok bhaiya... Pressed the bell bell icon already ♥️
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Tysm ❤️❤️❤️
@shanayagupta4002
@shanayagupta4002 Жыл бұрын
@@codestorywithMIK ohk got it
@guptashashwat
@guptashashwat Жыл бұрын
Guruji
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️🙏
@JJ-tp2dd
@JJ-tp2dd Жыл бұрын
bhai aj ka leetcode ni ayega?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Being uploaded. Taking some time. Stay tuned 😃
@niteshk0487
@niteshk0487 Жыл бұрын
kadanes max int sum = 0; int maxi = INT_MIN; for(int i=0; i
@tauquirahmed1879
@tauquirahmed1879 Жыл бұрын
I guess mini = min(mini, sum) If (sum>0) sum = 0
@niteshk0487
@niteshk0487 Жыл бұрын
@@tauquirahmed1879 bro sum>0 fir to always negative me ayega
@tauquirahmed1879
@tauquirahmed1879 Жыл бұрын
@@niteshk0487 yes you have to find the minimum know... Do it in two loops or take two sums, one to find minimum and one to find maximum. I am not sure it will work or not
@tauquirahmed1879
@tauquirahmed1879 Жыл бұрын
@@niteshk0487 my solution works. // code class Solution { public: int maxSubarraySumCircular(vector& nums) { int max_sum=INT_MIN, min_sum=INT_MAX, total_sum=0, circular_sum; int n = nums.size(); int curr_max_sum = 0, curr_min_sum = 0; for(int i = 0; i
@chitransh73
@chitransh73 3 ай бұрын
bruteforce class Solution { public: //This code will give TLE int Kadane(vector& nums) { int n=nums.size(); int maxsum=0; int sum=INT_MIN; for(int i=0;i
@piyushacharya7696
@piyushacharya7696 Жыл бұрын
support++
@ManinderSingh-xr6ir
@ManinderSingh-xr6ir Жыл бұрын
//Brute force, got tle after passing 102/111 test cases😅😅 int kadane(vector &arr,int n){ int maxi = INT_MIN; int sum = 0; for(int i = 0;i
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Good that you tried Brute force. Great job
@codestorywithMIK
@codestorywithMIK Жыл бұрын
And i liked your rotation part 👍🏻
@ManinderSingh-xr6ir
@ManinderSingh-xr6ir Жыл бұрын
@@codestorywithMIK Thank you sir. Learning a lot from your channel❤
@husler7424
@husler7424 Жыл бұрын
Why cant we just return directly "return max(circularMaxSum, maxSum);" instead of ?? if(maxSum > 0) return max(circularMaxSum, maxSum); return maxSum;
@codestorywithMIK
@codestorywithMIK Жыл бұрын
If you notice, I shared an example in my explanation : {-1,-1,-1} In cases like these, you will return wrong answer if you only do max(curcular, maxSum) Try to dry run this small case
@husler7424
@husler7424 Жыл бұрын
@@codestorywithMIK yes got it. So for handling all negative elements we are doing it right?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Indeed correct
@husler7424
@husler7424 Жыл бұрын
@@codestorywithMIK thank you 💜
@ghayoorhussain8930
@ghayoorhussain8930 Жыл бұрын
/** * Sir Brute force se TLE araha h but able to 102 testcases out of 111 */ class Solution { private int kadane(int[] arr) { int currSum = 0; int n = arr.length; int maxSum = Integer.MIN_VALUE; for(int i = 0; i < n; i++) { currSum += arr[i]; if (currSum > maxSum) maxSum = currSum; if (currSum < 0) currSum = 0; } return maxSum; } private void rotate(int arr[], int n) { int i, temp; temp = arr[0]; for (i = 0; i < n - 1; i++) arr[i] = arr[i + 1]; arr[i] = temp; } public int maxSubarraySumCircular(int[] nums) { int res = Integer.MIN_VALUE; int n = nums.length; for(int i = 0; i < n; i++) { rotate(nums, n); res = Math.max(res, kadane(nums)); } return res; } }
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes that’s expected because of the constraints. But i am glad you tried brute force also, it’s a good thing to practice because it helps in interviews
@alphadrones23
@alphadrones23 Жыл бұрын
I think the interviewer may stress optimizing it in a single pass...
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yep you can simply write all in a single pass. Click my code link in the description Both codes are available
@molyoxide8358
@molyoxide8358 Жыл бұрын
Bro Kadane's Algo se [5,-3,5] ka maxSum subarray 7 hoga na, 5 kaise??
@skillup638
@skillup638 Жыл бұрын
Yeah 7 but his mistake
@codestorywithMIK
@codestorywithMIK Жыл бұрын
My bad. That’s a silly. Thanks for pointing
@molyoxide8358
@molyoxide8358 Жыл бұрын
@@codestorywithMIK Bro Highlight karne ke point se nhi bola bura math maan na. Mujhe5 minute tak smj nhi aaya tha ki hua kaise. That's it par ab smj gya.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
No worries. Thanks for your feedback 😇
@coderletscode
@coderletscode 9 ай бұрын
Kadan's algo nhi karaye bhai
@adhikari669
@adhikari669 Жыл бұрын
Bhaya m algorithm kaha s or kaise pdu
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I think for Algorithm, you can go through GfG . That’s good But then make sure to solve Qns and apply algorithmic knowledge in them. If you get stuck in problems, feel free to visit my channel for clear explanations
@adhikari669
@adhikari669 Жыл бұрын
Kon kon s algorithm pdu?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
I would suggest first start basic topics like Array (it will have sorting algorithms, binary search etc) Then further advancing, sliding window algo etc. Basically, you need to choose a data structure to start with and then it will have certain different algorithms.
@adhikari669
@adhikari669 Жыл бұрын
Bhaya I can solve graph,tree problem , this kadanes algorithm that you mentioned without knowing about it I solved finding maximum subarray in result making my own sadanes algorithm,I did some greedy problem also,some divide and conquer.. I wanted to ask that ki is there a structured method of learning just like the dsa or its random just learning algorithms when encountering those questions
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Yes there is a structured way. First you should have cleared all algorithms based on Arrays Such as Binary Search Sliding Window Kadanes Algo is also based on Arrays and so on
@k-CE-OmkarPathak
@k-CE-OmkarPathak Жыл бұрын
awesome
Maximum Sum Circular Subarray - Leetcode 918 - Python
16:49
NeetCodeIO
Рет қаралды 38 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН
The Ultimate Sausage Prank! Watch Their Reactions 😂🌭 #Unexpected
00:17
La La Life Shorts
Рет қаралды 8 МЛН
Kadane's Algorithm | Maximum Subarray Sum | Finding and Printing
20:09
take U forward
Рет қаралды 487 М.
Maximum Sum Circular Subarray | Leetcode 918
16:34
Codebix
Рет қаралды 14 М.
Maximum Product Subarray - Best Intuitive Approach Discussed
20:27
take U forward
Рет қаралды 233 М.
Leetcode 918 Maximum Sum Circular Subarray ||CodingDecoded SDE sheet
13:29
Maximum Sum Circular Subarray | Leetcode #918
14:02
Techdose
Рет қаралды 87 М.
Ice Cream or Surprise Trip Around the World?
00:31
Hungry FAM
Рет қаралды 21 МЛН