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

Поділитися
Вставка
  • Опубліковано 8 січ 2025

КОМЕНТАРІ • 105

  • @codestorywithMIK
    @codestorywithMIK  2 роки тому +11

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

    • @nagmakhan3165
      @nagmakhan3165 2 роки тому

      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 2 роки тому +15

    Respect++
    Support++
    Your videos are addictive

  • @aadityaverma6539
    @aadityaverma6539 9 днів тому +1

    16:37 that is so clever . Hats off!

  • @divyangdheer7292
    @divyangdheer7292 2 роки тому +7

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

  • @mrunalwaghmare
    @mrunalwaghmare Місяць тому +1

    your videos explanation is best of all the videos i watched till now thank you sir!!!!!

  • @wooldan7007
    @wooldan7007 Рік тому +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.👍👍

  • @summisahani6415
    @summisahani6415 3 місяці тому +1

    Really impressed with your explanation. Thanks a lot. Keep posting.

  • @DeepakKumar-pd3lc
    @DeepakKumar-pd3lc 3 місяці тому +1

    Brilliant explanation>>>>>>>>>>>>>>>>>

  • @Badalkumar-me9ok
    @Badalkumar-me9ok 11 місяців тому +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.

  • @raisanjeeb42
    @raisanjeeb42 2 роки тому +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  2 роки тому

      ❤️❤️❤️

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому

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

  • @VsEdits59
    @VsEdits59 2 роки тому +5

    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  2 роки тому

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

  • @iamnoob7593
    @iamnoob7593 4 місяці тому +1

    WOW JUST WOW CRAZY GOOD EXPLANATION

  • @anmol3749
    @anmol3749 8 місяців тому +1

    Zabardastttt story building!!!!

  • @gui-codes
    @gui-codes 2 місяці тому

    you are just fantastic. deserves more subs 1 M

  • @shabananoor9423
    @shabananoor9423 2 роки тому +1

    Unbelievable explanation

  • @komalkrishna7836
    @komalkrishna7836 2 роки тому +1

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

  • @JJ-tp2dd
    @JJ-tp2dd 2 роки тому +1

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

  • @yashaggarwal825
    @yashaggarwal825 2 роки тому +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  2 роки тому +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

  • @zaviyakhurshid9097
    @zaviyakhurshid9097 2 роки тому +1

    Superb, very well explained 🔥

  • @arambh-gaur
    @arambh-gaur 4 місяці тому

    Great explaination !! Keep at it

  • @boomburstshorts2347
    @boomburstshorts2347 Рік тому +1

    Whoooaaa!!!!
    Solid Explanation Brother 🔥

  • @aditimahabole1761
    @aditimahabole1761 9 місяців тому +1

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

  • @gaurimandot5788
    @gaurimandot5788 11 місяців тому +1

    As always awesome sir😍

  • @sauravbiswajit8091
    @sauravbiswajit8091 Рік тому +1

    Ohhhhh bhai ....great explanation 😮

  • @alokgautam02
    @alokgautam02 2 роки тому +1

    Thanks for this awesome solution ..😇

  • @vaibhavgupta2130
    @vaibhavgupta2130 2 роки тому +1

    nice explanation...thanks

  • @sauravchandra10
    @sauravchandra10 Місяць тому +1

    Maza agya!

  • @yashisrivastava6196
    @yashisrivastava6196 Рік тому +1

    Nyc explanation 💯

  • @nagmakhan672
    @nagmakhan672 2 роки тому +1

    This is Art 🔥🔥🔥

  • @rguktiiit371
    @rguktiiit371 2 роки тому +3

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

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому

      Sure. Thanks for the suggestion
      Added to my list

    • @HimanshuPatel-wn6en
      @HimanshuPatel-wn6en 6 місяців тому

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

  • @Ramneet04
    @Ramneet04 Рік тому

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

  • @HARSHRAJ-gp6ve
    @HARSHRAJ-gp6ve Місяць тому +1

    nice bro

  • @ashishrawat8819
    @ashishrawat8819 10 місяців тому

    great video

  • @danishsinghjamwal627
    @danishsinghjamwal627 Рік тому +1

    awesome

  • @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  Рік тому +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 Рік тому

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

  • @tauquirahmed1879
    @tauquirahmed1879 2 роки тому +1

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

  • @chitransh73
    @chitransh73 5 місяців тому

    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

  • @ayushkumarsingh6762
    @ayushkumarsingh6762 Місяць тому +1

    Brute Force C++ code
    int maxSubarraySum(vector &arr) {
    int n = arr.size();
    int sum = 0;
    int maxSum = arr[0];
    for(int i = 0 ; i < n ; i++){
    sum += arr[i];
    maxSum = max(sum,maxSum);
    if(sum

  • @monugit9
    @monugit9 Місяць тому +1

  • @husler7424
    @husler7424 2 роки тому

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

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому

      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 2 роки тому +1

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

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому +1

      Indeed correct

    • @husler7424
      @husler7424 2 роки тому +1

      @@codestorywithMIK thank you 💜

  • @niteshk0487
    @niteshk0487 2 роки тому

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

    • @tauquirahmed1879
      @tauquirahmed1879 2 роки тому

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

    • @niteshk0487
      @niteshk0487 2 роки тому

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

    • @tauquirahmed1879
      @tauquirahmed1879 2 роки тому

      @@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 2 роки тому

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

  • @JJ-tp2dd
    @JJ-tp2dd 2 роки тому +1

    bhai aj ka leetcode ni ayega?

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому +1

      Being uploaded. Taking some time.
      Stay tuned 😃

  • @prnvsgr
    @prnvsgr 2 роки тому +1

    respect

  • @FanIQQuiz
    @FanIQQuiz Місяць тому

    Legend

  • @ManinderSingh-xr6ir
    @ManinderSingh-xr6ir 2 роки тому +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

  • @ghayoorhussain8930
    @ghayoorhussain8930 2 роки тому +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  2 роки тому +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

  • @tarifzaman
    @tarifzaman 11 місяців тому

    You are love

  • @coderletscode
    @coderletscode 11 місяців тому

    Kadan's algo nhi karaye bhai

  • @guptashashwat
    @guptashashwat 2 роки тому +1

    Guruji

  • @adhikari669
    @adhikari669 2 роки тому

    Bhaya m algorithm kaha s or kaise pdu

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому +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 2 роки тому

      Kon kon s algorithm pdu?

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому

      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 2 роки тому

      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  2 роки тому

      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

  • @apuljain6625
    @apuljain6625 2 місяці тому +1

    🤯

  • @molyoxide8358
    @molyoxide8358 2 роки тому

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

    • @skillup638
      @skillup638 2 роки тому +2

      Yeah 7 but his mistake

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому

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

    • @molyoxide8358
      @molyoxide8358 2 роки тому

      @@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  2 роки тому

      No worries.
      Thanks for your feedback 😇

  • @alphadrones23
    @alphadrones23 2 роки тому

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

    • @codestorywithMIK
      @codestorywithMIK  2 роки тому +2

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

  • @piyushacharya7696
    @piyushacharya7696 2 роки тому +1

    support++

  • @k-CE-OmkarPathak
    @k-CE-OmkarPathak Рік тому +1

    awesome