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

Sdílet
Vložit
  • čas přidán 28. 07. 2024
  • This is our 33rd Video on our Array Playlist.
    In this video we will try to solve a very very popular and good Qn "Maximum Sum Circular Subarray" (Leetcode-918)
    This is a Hard Version of Kadane's Algorithm, but I will make it easy for you. Trust me, watch this video.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Problem Name : Maximum Sum Circular Subarray
    Company Tags : Microsoft, Amazon
    My solutions on Github : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/maximum...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    0:00 - Intro and Understanding Qn
    2:10 - What is Kadane's Algorithm
    3:21 - Brute Force
    7:35 - Optimal Approach
    18:30 - steps
    20:05 - Revisiting Kadane's Algorithm
    22:36 - Final Story
    26:56 - Live Coding from Story
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
    #interviewpreparation #interview_ds_algo #hinglish

Komentáře • 93

  • @codestorywithMIK
    @codestorywithMIK  Před rokem +9

    Small correction at 3:40
    Answer for {5, -3, 5} will be 7

    • @nagmakhan3165
      @nagmakhan3165 Před rokem

      Got it

    • @navinojha5637
      @navinojha5637 Před rokem

      Why bhaiya it should be 10 only correct ? because if we rotate (5, 5, -3) maximum will be 10 right ?

    • @navinojha5637
      @navinojha5637 Před rokem

      sorry got it, I mistaked it with normal subarray sum

  • @souravjoshi2293
    @souravjoshi2293 Před rokem +9

    Respect++
    Support++
    Your videos are addictive

  • @divyangdheer7292
    @divyangdheer7292 Před rokem +3

    college walo nai ghanta kuch nhi padhaya kaden's
    humne khud pdha hai aap jaise youtubers ki help se

  • @alokgautam02
    @alokgautam02 Před rokem +1

    Thanks for this awesome solution ..😇

  • @shabananoor9423
    @shabananoor9423 Před rokem +1

    Unbelievable explanation

  • @Badalkumar-me9ok
    @Badalkumar-me9ok Před 5 měsíci +1

    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.

  • @wooldan7007
    @wooldan7007 Před 10 měsíci +1

    Really!! man at 8:00 time stamp my search for actual reason gets over. You are always truly amazing to highlight such important details.👍👍

  • @nagmakhan672
    @nagmakhan672 Před rokem +1

    This is Art 🔥🔥🔥

  • @gaurimandot5788
    @gaurimandot5788 Před 6 měsíci +1

    As always awesome sir😍

  • @komalkrishna7836
    @komalkrishna7836 Před rokem +1

    Wow! Great explanation sir👏
    Your explanation is awesome as always,
    Thank you 😊

  • @boomburstshorts2347
    @boomburstshorts2347 Před rokem +1

    Whoooaaa!!!!
    Solid Explanation Brother 🔥

  • @vaibhavgupta2130
    @vaibhavgupta2130 Před rokem +1

    nice explanation...thanks

  • @anmol3749
    @anmol3749 Před 3 měsíci +1

    Zabardastttt story building!!!!

  • @zaviyakhurshid9097
    @zaviyakhurshid9097 Před rokem +1

    Superb, very well explained 🔥

  • @ashishrawat8819
    @ashishrawat8819 Před 5 měsíci

    great video

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak Před 8 měsíci +1

    awesome

  • @yashisrivastava6196
    @yashisrivastava6196 Před 10 měsíci +1

    Nyc explanation 💯

  • @sauravbiswajit8091
    @sauravbiswajit8091 Před rokem +1

    Ohhhhh bhai ....great explanation 😮

  • @rguktiiit371
    @rguktiiit371 Před rokem +3

    Bro make a video on time complexities and how to properly read the constraints mentioned in the question

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      Sure. Thanks for the suggestion
      Added to my list

    • @HimanshuPatel-wn6en
      @HimanshuPatel-wn6en Před měsícem

      @@codestorywithMIK Bhaiyia Please make video on how to read constraints and understand that which tc solution will work

  • @yashaggarwal825
    @yashaggarwal825 Před rokem +1

    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  Před rokem +2

      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

  • @tarifzaman
    @tarifzaman Před 5 měsíci

    You are love

  • @JJ-tp2dd
    @JJ-tp2dd Před rokem +1

    So so good bhai..mujhe bhulna ni sb millions subscribers honge apke

  • @aditimahabole1761
    @aditimahabole1761 Před 3 měsíci +1

    muje aapke charan chune ....aap itnaa achaa kese padhaaa sakteeeeeeee.

  • @prnvsgr
    @prnvsgr Před rokem +1

    respect

  • @raisanjeeb42
    @raisanjeeb42 Před rokem +1

    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  Před rokem

      ❤️❤️❤️

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      I am so glad Sanjeeb that you tried on your own and gave it time. That’s how we learn. Keep it up

  • @Ramneet04
    @Ramneet04 Před 9 měsíci

    We can also do if (circular sum==0)return maxsum;
    Else return max(maxsum,circularsum);

  • @vishalplayzz2580
    @vishalplayzz2580 Před rokem +4

    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  Před rokem

      Glad you tried brute force. It’s always a good practice to go for brute force

  • @Ramneet04
    @Ramneet04 Před 9 měsíci

    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  Před 9 měsíci +1

      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 Před 9 měsíci

      @@codestorywithMIK 🙃 Btw explanation is top notch no doubt 👉👈

  • @AdityaSingh-ui4tr
    @AdityaSingh-ui4tr Před rokem

    In [5,-3,5] this case 5+(-3)+5+5 can also be a subarray

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +3

      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 Před rokem +1

    Bhaiya topic tags mein Dynamic Programming aur Monotonic Queue q diya h? Usse bhi koi approach ho skta h kya?

  • @guptashashwat
    @guptashashwat Před rokem +1

    Guruji

  • @piyushacharya7696
    @piyushacharya7696 Před rokem +1

    support++

  • @JJ-tp2dd
    @JJ-tp2dd Před rokem +1

    bhai aj ka leetcode ni ayega?

  • @niteshk0487
    @niteshk0487 Před rokem

    kadanes max
    int sum = 0;
    int maxi = INT_MIN;
    for(int i=0; i

    • @tauquirahmed1879
      @tauquirahmed1879 Před rokem

      I guess
      mini = min(mini, sum)
      If (sum>0)
      sum = 0

    • @niteshk0487
      @niteshk0487 Před rokem

      @@tauquirahmed1879 bro sum>0 fir to always negative me ayega

    • @tauquirahmed1879
      @tauquirahmed1879 Před rokem

      @@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 Před rokem

      @@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

  • @coderletscode
    @coderletscode Před 5 měsíci

    Kadan's algo nhi karaye bhai

  • @molyoxide8358
    @molyoxide8358 Před rokem

    Bro Kadane's Algo se [5,-3,5] ka maxSum subarray 7 hoga na, 5 kaise??

    • @skillup638
      @skillup638 Před rokem +2

      Yeah 7 but his mistake

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      My bad. That’s a silly.
      Thanks for pointing

    • @molyoxide8358
      @molyoxide8358 Před rokem

      @@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  Před rokem

      No worries.
      Thanks for your feedback 😇

  • @adhikari669
    @adhikari669 Před rokem

    Bhaya m algorithm kaha s or kaise pdu

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +1

      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 Před rokem

      Kon kon s algorithm pdu?

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      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 Před rokem

      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  Před rokem

      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

  • @husler7424
    @husler7424 Před rokem

    Why cant we just return directly "return max(circularMaxSum, maxSum);" instead of ??
    if(maxSum > 0)
    return max(circularMaxSum, maxSum);
    return maxSum;

    • @codestorywithMIK
      @codestorywithMIK  Před rokem

      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 Před rokem +1

      @@codestorywithMIK yes got it. So for handling all negative elements we are doing it right?

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +1

      Indeed correct

    • @husler7424
      @husler7424 Před rokem +1

      @@codestorywithMIK thank you 💜

  • @ghayoorhussain8930
    @ghayoorhussain8930 Před rokem +1

    /**
    * 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  Před rokem +1

      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

  • @ManinderSingh-xr6ir
    @ManinderSingh-xr6ir Před rokem +2

    //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

  • @alphadrones23
    @alphadrones23 Před rokem

    I think the interviewer may stress optimizing it in a single pass...

    • @codestorywithMIK
      @codestorywithMIK  Před rokem +2

      Yep you can simply write all in a single pass.
      Click my code link in the description
      Both codes are available

  • @danishsinghjamwal627
    @danishsinghjamwal627 Před rokem +1

    awesome