Maximum Sum Circular Subarray | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 2 พ.ย. 2024

ความคิดเห็น •

  • @techdose4u
    @techdose4u  4 ปีที่แล้ว +39

    In the 3rd METHOD...you need to take MAX(inversion_answer, Kadane's_answer) to get correct result. This i have explained for 4th METHOD where i implemented the logic of Kadane's algorithm. The aim of first 3 methods was just to give you an idea about other possible solutions and hence are not explained in depth :)

    • @sureshgarg920
      @sureshgarg920 4 ปีที่แล้ว +5

      Yes , For this method's there will be two possibilities either we will requires Wrapping or not ...
      So you have explained only that case when we will require Wrapping but for the non wrapping case we also have to calculate the max_sum_subarray for the normal array too...
      And then maximum of the two will be our answer :)

    • @LegitGamer2345
      @LegitGamer2345 3 ปีที่แล้ว

      @@sureshgarg920 thank u boi

    • @harshithmundada5100
      @harshithmundada5100 ปีที่แล้ว

      Won't the third method fail for [-1,-2,-3]

    • @nitinvadadoriya8280
      @nitinvadadoriya8280 ปีที่แล้ว +1

      @@harshithmundada5100 MAX(inversion_answer, Kadane's_answer) before cheeking this condition you have to check if inversion_answer == 0 then our answer will be kadane`s _answer, other wise return MAX(inversion_answer, Kadane's_answer).

  • @lings628
    @lings628 4 ปีที่แล้ว +32

    We have a new baller in town! What sets you apart from others is you start explaining from the most basic naive solution, all the way to the most effective solution. Please keep doing that. Everyone can explain the solution, but that is not what we need. How to approach to the solution is key, you are killing it. Huge fan! Thank you very much!

  • @mayurkoli4145
    @mayurkoli4145 4 ปีที่แล้ว +22

    Here i am struggling with thinking single solution, and u came up with 4 solutions 😑😑how ur brain works.

  • @Udemys
    @Udemys 3 หลายเดือนก่อน

    It's a typical solution that we know the answer and solution first and then explain the problem

  • @apoorv7361
    @apoorv7361 3 ปีที่แล้ว +5

    sir third approach will not work for this test case [1,-2,3,-2]

  • @santhoshreddy6505
    @santhoshreddy6505 4 ปีที่แล้ว +11

    Actually there is a mistake in the solution, You need to take the maximum of kadane's algo and inverted kadane's where I mean by inverted kadane's is the third method explained in the video.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Actually the 2 cases were shown properly in 4th method. The first 3 methods were just alternate solution ideas because I only implemented the 4th method. In the 1st and 2nd method it was not required but in the 3rd method we need to take max of both the cases as pointed out by you (like we did in method 4). Thanks.

    • @KoushikChowdhury2016
      @KoushikChowdhury2016 4 ปีที่แล้ว

      @@techdose4u Here is the C++ code
      class Solution {
      public:
      int maxSubarraySumCircular(vector& A) {
      int n=A.size();
      int curr_sum=A[0], max_sum=A[0];
      for(int i=1;i

    • @evgeni-nabokov
      @evgeni-nabokov 4 ปีที่แล้ว +2

      @@KoushikChowdhury2016 it is not compiled. I counted 4 syntax mistakes. Even after fixing mistakes it gives a wrong answer for [-2,-3,-1]. Also the solution has many loops. The answer can be calculated using 1 loop.

  • @vimarshkoul930
    @vimarshkoul930 3 ปีที่แล้ว +6

    sir you mentioned in the second solution that the max subarray sum is total array sum - min subarray sum. But I guess it should be
    max( maximum subarray sum using kadane's Algo, total array sum - min subarray sum). Because then only the algo will work properly on test cases
    like 1, -2, 3, -2. Please correct if I am wrong. Thanks!

    • @bruteforceman2311
      @bruteforceman2311 2 ปีที่แล้ว

      he is right bro and you are wrong. At first you have to calculate total sum of that array which is 0 in your test case then you have to subtract the minimum subarray value which is -2 in your test case so 0-(-2)=2 . and it is the answer. You cant go to the first position from last position while you are calculating minimum subarray value.

  • @manthankhorwal1477
    @manthankhorwal1477 4 ปีที่แล้ว +4

    this is a great video . I actually did this problem by making prefix sum array but actually waiting for your video for some more approachs

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Thanks :)

    • @abdulbasith7911
      @abdulbasith7911 4 ปีที่แล้ว +1

      Could you share with your prefix array solution.please?

  • @NikhilKumar-oy7mx
    @NikhilKumar-oy7mx 4 ปีที่แล้ว +6

    really nice. I liked that changing sign solution. You are great, you came up with these many solutions.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +3

      I had implemented 2nd solution and came up with first 3 solutions but the 2nd solution code looked messy. I saw 4th solution and again implemented the question using 4th one :D I hope these techniques were helpful.

    • @rayanahmed1122
      @rayanahmed1122 4 ปีที่แล้ว +3

      Not sure the changing sign method works for every case. You mentioned one of the boundary cases when all the elements are negative. The counterpart to that would be when all are positive too, we should sum everything up. However, there is still one case that I am struggling to wrap my head around: eg (-2,3,-2). According to your solution, we would get 1, but the actual solution is 3. Let me know if I am missing something. Removing only one instance of the min sub-array (if there are multiple instances) will not suffice.

  • @pratikkedar4476
    @pratikkedar4476 2 ปีที่แล้ว

    demystify Confusion :The ans = array_sum - min_sub_array_sum is incomplete so to say,
    Consider this test cased (listed on leet code) : [1, -2, 3, -2] ans should be 3 but as per above explained algo its coming 2.
    Now, to handle this we can say my final ans should be max(array_sum - min_sub_array_sum, max_sub_array_sum)
    Also as explained correctly in video there is edge case (all negative elements) in this case return max value.
    y Yes, we can traverse only once and do all the checks/computation in single pass, nothing spl.
    TC O(n) SC O(1) solution :
    int maxSubarraySumCircular(vector& nums) {
    int local_min(nums[0]),global_min(nums[0]);
    int local_max(nums[0]),global_max(nums[0]);
    int total_sum = nums[0];
    int negEleCount = nums[0] < 0 ? 1 : 0;
    int largestElement = nums[0];
    for(int i=1;i

  • @pranavkul525
    @pranavkul525 2 ปีที่แล้ว +2

    Wonderful explanation! It's rare to find such well-elaborated content regarding coding questions on YT, so it's great you are doing so good in it

  • @yashgupta3127
    @yashgupta3127 4 ปีที่แล้ว +2

    In your alternative method I guess you forgot to tell one thing if. Temp_maxsum

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Maybe 😅 Is it present in code?

    • @yashgupta3127
      @yashgupta3127 4 ปีที่แล้ว

      @@techdose4u ohh my bad you are making tempsum=0 if sum negetive that will do the trick.sorry.

  • @mahnazakbari2504
    @mahnazakbari2504 2 ปีที่แล้ว

    Thanks for great effort. Something that is missing is WHY any of these methods are "correct".

    • @SumitSharma-cq2cf
      @SumitSharma-cq2cf 2 ปีที่แล้ว

      Hello Mahnaz, Could you please help me to understand Sol-2, "Find Max SubArray Sum with Max Window Size-N", I could not understand his approach- How he is moving Left and Right pointers. Thank You in Advance

  • @kashifanwar9733
    @kashifanwar9733 4 ปีที่แล้ว +1

    amazing ...i learnt 2 new methods today

  • @ayushkumarjha5493
    @ayushkumarjha5493 ปีที่แล้ว

    I understood your explanation very well, but what gives the intuition to approach this problem like this. please help me develop such intuition

  • @sunnysam69
    @sunnysam69 4 ปีที่แล้ว +6

    Okay. Great video. Though i have some queries.
    1. In solution 3, instead of inverting numbers(which confuses me) , we could tweak kadane's algo to find the min subarray sum. That sould work right as per the observations at 05:05
    2. If my input is [-1, 5, 5, - 1]
    Min subarray sum = - 1
    Total array sum = 8
    Hence, max subarray sum = 8-(-1) = 9
    Shouldn't the answer be 10?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Thanks. Please read the pinned COMMENT by me. You will get your answer :)

    • @SinghFlex
      @SinghFlex 4 ปีที่แล้ว +5

      You have to find the max sum using kadanes algo ,and compare that max sum with the max sum via his approach, and return max ,out of both.
      Here max sum kadanes algo 5+5 =10
      Tech dose explanation:
      Sum of total array =8
      Invert signs and find max sum subarray we get 1 as the value.
      (sum-invert val) = 8-1 =7
      Max(10,7) =10

    • @HimanshuSharma-tm2ms
      @HimanshuSharma-tm2ms 4 ปีที่แล้ว +1

      @@SinghFlex thanks for the clear explanation !! there's a mistake thought (sum-invert val)=8-(-1)=9 should be the correct thing.

    • @raisanjeeb42
      @raisanjeeb42 2 ปีที่แล้ว +3

      @@SinghFlex Dude...What an explanation 🙌🙌Thanks

  • @mashab9129
    @mashab9129 ปีที่แล้ว

    best explanation of this problem. thank you

  • @rajaganji7982
    @rajaganji7982 2 ปีที่แล้ว +6

    Method 2 does'nt seem to work on testcase : [1,-2,3,-2], Expected answer is 3. but we get 0(total sum) - -2(maxNegative). so we end up getting 2.

    • @rahulagarwal3611
      @rahulagarwal3611 2 ปีที่แล้ว

      return max(maxssum,arsum-minssum);
      you can return this instead to get correct answer

    • @SumitSharma-cq2cf
      @SumitSharma-cq2cf 2 ปีที่แล้ว

      Hello Raja, Could you please help me to understand Sol-2, "Find Max SubArray Sum with Max Window Size-N", I could not understand his approach- How he is moving Left and Right pointers. Thank You in Advance

    • @rajaganji7982
      @rajaganji7982 2 ปีที่แล้ว +1

      @@SumitSharma-cq2cf It's a sliding window technique. It is a very famous technique. Just google it.

    • @SumitSharma-cq2cf
      @SumitSharma-cq2cf 2 ปีที่แล้ว

      @@rajaganji7982 Sure Thank You I will check this

  • @srividhyaparthasarathy5401
    @srividhyaparthasarathy5401 4 ปีที่แล้ว +2

    explanation was really osm ...But sir Some test cases fail when i used MAX(inversion_answer, Kadane's_answer) in third method.I hope everyone finds third one to be ec,can u pls code third one in c++ :)

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I think some people have already done it. So have a look at their code. You might understand your mistake.

    • @srividhyaparthasarathy5401
      @srividhyaparthasarathy5401 4 ปีที่แล้ว

      @@techdose4u thank u so much sir ...u r really too patient to answer all of ur students..hats off:)

  • @ayushmansharma3293
    @ayushmansharma3293 3 ปีที่แล้ว +1

    Really interesting solution.

  • @anushree3744
    @anushree3744 4 ปีที่แล้ว +8

    I actually tried to do the 2nd approach. we have to make sure that we dont process the same element twice.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +4

      I also applied the 2nd approach initially 😅

    • @yashgoswami5374
      @yashgoswami5374 ปีที่แล้ว

      @@techdose4u later you found soln on GFG?

  • @sazzadhossain5999
    @sazzadhossain5999 3 ปีที่แล้ว +2

    [-5, 1, -5] Here min-straight-sum = total sum = -9. But from this we can not say that all elements are negative. I think your method 4 need to be updated.

    • @eyeamkd
      @eyeamkd 2 ปีที่แล้ว

      irrespective of whether all elements are negative, returning the max_sum value if the condition min_straight_sum == total_sum true works

  • @pratyushukla
    @pratyushukla ปีที่แล้ว +1

    Nicely explained 😄

  • @svenkatesh7787
    @svenkatesh7787 3 ปีที่แล้ว +1

    3rd Method
    int kadan(vector nums){
    int sum=nums[0];
    int maxs=nums[0];
    int n=nums.size();
    for(int i=1;i

  • @bhargav1811
    @bhargav1811 ปีที่แล้ว +2

    what if minimum sub array sum is in circular subarray like - [1,-2,3,-2]?
    min sub array sum = -2 and total is 0 so answer=>total - min sub array sum= 0-(-2)= 2 which is wrong !!
    Correct answer is 3!!! Plz help me understand this? Who else came here to solve after seeing 18 th jan problem of day?? :)

  • @jayeshborase7900
    @jayeshborase7900 3 ปีที่แล้ว +1

    It's amazing explanation 👍🤩🙏

  • @nishantsinghraj7098
    @nishantsinghraj7098 4 ปีที่แล้ว +3

    Lot of people getting confused about method 3, I have implemented it as explained(with correction), it's a nice approach, kudos to @TECHDOSE , here is the solution using method 3. I know I used some extra space, but it looks more clear this way!
    int maxSubarraySumCircular(vector& A) {
    int sz=A.size();
    bool allNegative=true;
    int totalSum=A[0],minAns=A[0],maxAns=A[0],maxallNegative=A[0];
    int dp1[sz],dp2[sz];
    memset(dp1,0,sizeof(dp1));
    memset(dp2,0,sizeof(dp2));
    dp1[0]=A[0],dp2[0]=A[0];
    if(A[0]>0)
    allNegative=false;
    for(int i=1;i0)
    allNegative=false;

    dp1[i]=min(dp1[i-1]+A[i],A[i]);
    dp2[i]=max(dp2[i-1]+A[i],A[i]);
    minAns=min(minAns,dp1[i]);
    maxAns=max(maxAns,dp2[i]);
    maxallNegative=max(maxallNegative,A[i]);
    totalSum+=A[i];
    }
    if(allNegative)
    return maxallNegative;
    else
    return max(maxAns,(totalSum-minAns));
    }

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Thanks for sharing

  • @evgeni-nabokov
    @evgeni-nabokov 4 ปีที่แล้ว +2

    Let's say Kadane's_answer = a, inversion_answer = b, then the corrent answer for the sol. 3 is: a if a < 0 else MAX(a, b).

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes correct. This is shown in the code for method 4

  • @ALEEMKHAWAR1
    @ALEEMKHAWAR1 2 ปีที่แล้ว

    Great explanation

  • @arvindgupta-zm7lz
    @arvindgupta-zm7lz 3 ปีที่แล้ว

    1st approach index % N will give us end point? if yes then for(int i=0;i

  • @glazeds0n
    @glazeds0n 4 ปีที่แล้ว +2

    Thank u sir,,, great explanation as usual

  • @tawhidurrahaman5782
    @tawhidurrahaman5782 ปีที่แล้ว

    Your explanation could not be any better

  • @wan6ify
    @wan6ify 2 ปีที่แล้ว +1

    Amazing!

  • @EditorGuru
    @EditorGuru 3 ปีที่แล้ว

    amazing explanation...can you please tell why we need to take max(max_Straight_sum, total_sum - min_straight_Sum) ?

    • @trevorPhillips284
      @trevorPhillips284 3 ปีที่แล้ว

      It the answer is within the initial array then the answer would be maximum sum subarray, therefore the max(max_Straight_sum, total_sum - min_straight_Sum) is taken. Watch from 1:10

  • @NatureLover-oq6uc
    @NatureLover-oq6uc ปีที่แล้ว

    YOU ARE JUST AMAZING....MY DREAM TO MEET YOU.

  • @wenyangzhang7605
    @wenyangzhang7605 4 ปีที่แล้ว +3

    The third method doesn't work for [-3,5,6,-1,4,-2]?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Which method? The 3rd? You need to take max(inversion_answer,kadane's answer)

    • @wenyangzhang7605
      @wenyangzhang7605 4 ปีที่แล้ว +2

      Ahh I got it. Thank you so much!

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Welcome :)

  • @prabhatbhartola9191
    @prabhatbhartola9191 2 ปีที่แล้ว +1

    5:32, This dosen't work for all test cases. One of them is {-3, -18, -22, -21, -17, 16, -14, 28, -22}, The correct result is 30.

  • @rishabhrai8404
    @rishabhrai8404 2 ปีที่แล้ว

    Amazing solution thanks

  • @venusisalwaysabove
    @venusisalwaysabove ปีที่แล้ว

    outstanding explanation ! Thank you !

  • @Yash-uk8ib
    @Yash-uk8ib 4 ปีที่แล้ว +2

    what is the idea behind method 3? plzz explain ur thought process

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +4

      Idea for method 3 and 4 are same. Maximum sum subarray = Array sum - minimum sun subarray (with boundary cases as exceptions). Now, array sum is easy to find. How do you find min sum subarray? You can use kadane's algorithm only to find max and not min. So if you invert sign of every number then the problem changes to finding max sum subarray. Find using kadane's and again invert the sum result to get your min sum subarrays value. You have array sum as well. So, max sum subarray with wrap around is easy breezy now :)

    • @kollivenkatamadhukar5059
      @kollivenkatamadhukar5059 3 ปีที่แล้ว +1

      @@techdose4u U went off-topic he and even I am asking now, how is it working. How doing like that is giving us the correct answer
      PS: I am not asking to explain your approach or the algorithm run through. Maximum sum subarray = Array sum - minimum sun subarray (with boundary cases as exceptions). This we can do for standard max sum subarray question right so why are we using the other algo instead of this(I mean the same algo with the different idea) and why this now and how did u get this.

    • @kollivenkatamadhukar5059
      @kollivenkatamadhukar5059 3 ปีที่แล้ว +1

      you must correct Max sum subarray with max sum circular subbaray

  • @tanishksharma7339
    @tanishksharma7339 4 ปีที่แล้ว

    Can you please tell me why this is failing test cases when I followed method 3 and even used max(inversion_answer, Kadane's_answer)
    public static void main (String[] args)throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int cases = Integer.parseInt(br.readLine().trim());
    while(cases-->0){
    int n = Integer.parseInt(br.readLine().trim());
    int[] arr = new int[n];
    int arrSum = 0;
    String ip[] = br.readLine().trim().split(" ");
    for(int i=0;i

  • @satyareddy8671
    @satyareddy8671 ปีที่แล้ว

    super brooo

  • @girikgarg8
    @girikgarg8 ปีที่แล้ว +1

    Done!

  • @utkarshraj9061
    @utkarshraj9061 4 ปีที่แล้ว +1

    thanks a lot for explanation, when you discuss the third method i thought this is it but there is an extra chk which you discuss later on. i.e. which of the two is max normal kadane's or wrapping part.

  • @akhilkhubchandani2632
    @akhilkhubchandani2632 4 ปีที่แล้ว +1

    Very well explained . Thank you so much !

  • @manthankhorwal1477
    @manthankhorwal1477 4 ปีที่แล้ว +1

    we have to compare normal kadane answer with the inverted kadane and return maximum of them

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Yes correct. This is true for method 3 and 4.

  • @TheTushar35
    @TheTushar35 4 ปีที่แล้ว +2

    How do you got to the observation that we need to take a difference of total sum with min-sum subarray?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +5

      It is very logical na. It was sure that you needed to use kadane's. Some other questions also use these observations. So, if you have seen those questions or solutions then probably this will also natural come to mind. As I keep saying, it's all about practice.

    • @tusharchaturvedi100
      @tusharchaturvedi100 3 ปีที่แล้ว

      @@techdose4u I still not understood plz give some examples to prove this statement

  • @leetcodeproblems1169
    @leetcodeproblems1169 2 ปีที่แล้ว

    How did you come up with the idea for 3rd or 4th solution?

  • @Gauravkr0071
    @Gauravkr0071 4 ปีที่แล้ว +1

    Amazing bro

  • @Star_Bawa9
    @Star_Bawa9 2 ปีที่แล้ว

    Just concat one array with the reverse of the same array and apply kdanes algo .... Guess it should wordk

  • @carloscruzado4775
    @carloscruzado4775 2 ปีที่แล้ว +1

    Third method: (JAVA)
    class Solution {
    public int maxSubarraySumCircular(int[] nums) {
    int n=nums.length;
    int minS=100000;
    int maxS=-100000;
    int currMaxS=0;
    int currMinS=0;
    int total=0;
    boolean check=false;
    boolean allNegative=true;
    for(int i=0;i=0) allNegative=false;
    // sum of all numbers
    total+=nums[i];
    // min subarray
    if(currMinS>0) currMinS=0;
    currMinS+=nums[i];
    minS=Math.min(minS,currMinS);
    // max subarray
    if(currMaxS

    • @SumitSharma-cq2cf
      @SumitSharma-cq2cf 2 ปีที่แล้ว

      Hello Carlos, Could you please help me to understand Sol-2, "Find Max SubArray Sum with Max Window Size-N", I could not understand his approach- How he is moving Left and Right pointers. Thank You in Advance

    • @carloscruzado4775
      @carloscruzado4775 2 ปีที่แล้ว

      @@SumitSharma-cq2cf I don't know either.

  • @Tarunkumar-om3hu
    @Tarunkumar-om3hu 3 ปีที่แล้ว +2

    Can we just find the kadane of 2 concatenation

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      I don't think so. The max subarray found must also be contiguous.

  • @SurajKumar-cg1mm
    @SurajKumar-cg1mm 2 ปีที่แล้ว

    Hii bro can u please recommends any algorithms books that will more helpful to learn algorithms better..

  • @MKSundaram
    @MKSundaram 3 ปีที่แล้ว

    I tried your alternative method and it worked on first go itself. I tried to understand why it works? and failed to understand the logic. could you please help?

  • @akhildeepshrivastav
    @akhildeepshrivastav 4 ปีที่แล้ว +2

    What’s software do you use to record screen and free hand typing

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Camtasia + Wacom

  • @gigachad6844
    @gigachad6844 2 ปีที่แล้ว +1

    Using your method:
    Array: [1,-2,3,-2]
    Minimum subarray sum=-2
    total array sum=0
    max subarray sum=0-(-2) = 2
    Correct answer = 3 NOT 2

  • @sarthakshukla5251
    @sarthakshukla5251 3 ปีที่แล้ว +1

    In the last approach, why are we comparing tmp_max and tmp_min to zero?

  • @vivekgagrani8288
    @vivekgagrani8288 2 ปีที่แล้ว

    For input array (-5, -3, 2, -4) , the above logic not working. Total array sum will be -10 and min array sum will be -10 and if we subtract them -10-(-10) then it equals 0 .
    But max array sum should be 2.
    Am I missing anything. Please help

  • @ryanryan6567
    @ryanryan6567 4 ปีที่แล้ว +1

    How You came up with this kinda logic like in method 3 and 4?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I knew 3 methods. I took the 4th method from best time solution after submitting. I just dry ran on several examples and I explained the logic what I got.I wanted to discard this from including in the video, but I still included it 😅.This type of algos are not algos but tricks and it won't help during interviews. So I am not a fan of method 4. But method 3 is very common and intuitive. Method 3 is very useful for many problems.

  • @bhargavreddy5938
    @bhargavreddy5938 4 ปีที่แล้ว

    In the 2nd method-- min sub-array sum and total array sum if the input is [5,-3,2,-6,-1,4] the min sub array will be =-7 and total sum will be -1 which is equal to -8, which is wrong. XXXXX.

  • @aniruddhachattopadhyay611
    @aniruddhachattopadhyay611 4 ปีที่แล้ว

    Could you tell what software you use to write while explaining the concepts. Its really neat and helpful for keeping notes. Great video by the way.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Wacom tablet + software

  • @mysteriousocean3206
    @mysteriousocean3206 3 ปีที่แล้ว +1

    fantastic

  • @dipanshusharma9084
    @dipanshusharma9084 2 ปีที่แล้ว +1

    approach 3 is failing for the test case : -3 -18 -22 -21 -17 16 -14 28 - 22

  • @jaatharsh
    @jaatharsh 3 ปีที่แล้ว

    please check ur final soln with arr = [-10,-7,9,-7,6,9,-9,-4,-8,-5] Expected ans: 17, i guess above algo will return 9
    won't it be better to keep a boolean variable initially set false 'onePositiveFound' to indicate at least 1 non-negative value was found in the array and return final ans based upon it?

  • @Udemys
    @Udemys 3 หลายเดือนก่อน

    how is this going to help me learn code and prove that I am a good coder?

  • @christopherjiang30
    @christopherjiang30 2 ปีที่แล้ว

    genius algorithms

  • @kshakilaa7022
    @kshakilaa7022 4 ปีที่แล้ว +1

    Thank you so much ....

  • @zod1ack_csgo_clips425
    @zod1ack_csgo_clips425 3 ปีที่แล้ว

    How is first approach O(n^^2) ? Shouldnt it be O(2n)?

  • @rohithkumartangudu4664
    @rohithkumartangudu4664 4 ปีที่แล้ว +2

    3rd method is failing for the input 1,-2,3,-2

    • @AKASHKUMAR-we5hg
      @AKASHKUMAR-we5hg 4 ปีที่แล้ว +2

      No dude it's working I tried it

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +2

      Actually this is working with what I explained. NOTE: You need to take max of both cases. I just showed the first case and missed the 2nd case. The 2 cases are max subarray sum in array without wrap and 2nd one is max subarray sum with wrap around. We take max of both to return as answer. You can see that for method 4 I did include both cases while returning and I explained in code as well. In the same way, you need to do for method 3 as well. I just roughly explained it to give the idea.

    • @AnkitKumarCoder
      @AnkitKumarCoder 4 ปีที่แล้ว +1

      @@techdose4u
      // appliying Kadane's algorithm with inversion method
      int max_sum = A[0], vec_sum = 0, inversion_sum = 0, ends_here = 0;
      for(int i = 0; i < A.size(); i ++){
      // array sum
      vec_sum += A[i];
      // inversion sum of array
      inversion_sum += (A[i]*-1);

      // below algo is completely same as Kadane's algorithm
      ends_here = ends_here + A[i];
      if(ends_here > max_sum)
      max_sum = ends_here;
      if(ends_here < 0)
      ends_here = 0;
      }
      cout

    • @siddharthkela1818
      @siddharthkela1818 4 ปีที่แล้ว

      @@AKASHKUMAR-we5hg Can you explain how. Even I think its not working .

  • @pallavsharma9110
    @pallavsharma9110 3 ปีที่แล้ว

    1st method (O(n^2)) solution
    #include
    #include
    using namespace std;
    int kadane(int start,int n,int a[]){
    int curr_max=a[start];
    int global_max=a[start];
    start=start+1;
    for(int i=1;in-1) {
    start=start%n;
    }
    curr_max=max(a[start],curr_max+a[start]);
    global_max=max(global_max,curr_max);
    start++;
    }
    return global_max;
    }
    int main()
    {
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i>a[i];
    }
    int max_circular_subarr_sum=INT_MIN,start;
    for(int i=0;i

  • @AkashYadav-zk8nx
    @AkashYadav-zk8nx 4 ปีที่แล้ว

    I think your inverted solution might not work properly for the all postive no.s case so we might also consider it as a special or something???

  • @yitingg7942
    @yitingg7942 3 ปีที่แล้ว +2

    This one is so hard to implement!!

  • @kunalgupta3665
    @kunalgupta3665 4 ปีที่แล้ว

    Ithink it fails here {4,-1,2,6,-1,7,1,-10,5} suppose this array here min subArraySum =-10 and arrSum=5 now it will give maxSubArraySum=15 but the ans in 9 you should take sum of non-contributing elements instead of minSubArraysum. sum of non-contributing elements=-4 arrSum=5 so ans is 9 which is correct.

  • @adityapandey9639
    @adityapandey9639 3 ปีที่แล้ว

    This algorithm I understood but still confused how this works like Y we're doing all this?

  • @sdeBack1
    @sdeBack1 3 ปีที่แล้ว +1

    Something new fot me

  • @sagnikbhattacharya1202
    @sagnikbhattacharya1202 4 ปีที่แล้ว +1

    u my dronacharya

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      I am honoured 😁

  • @nickgaur3324
    @nickgaur3324 3 ปีที่แล้ว

    what is the difficulty level of this question ??

  • @briankarcher8338
    @briankarcher8338 2 ปีที่แล้ว

    I tried option 2. Seemed logical. Until I realized there wasn't a clear solution of what to do when the sum gets reset to 0. The left pointer becomes invalid. Oops! Tried to work around the issue, ended up with a mess.

    • @SumitSharma-cq2cf
      @SumitSharma-cq2cf 2 ปีที่แล้ว

      Hello, Could you please help me to understand Sol-2, "Find Max SubArray Sum with Max Window Size-N", I could not understand his approach- How he is moving Left and Right pointers. Thank You in Advance

  • @tours5248
    @tours5248 4 ปีที่แล้ว

    Bro at 5.56 time your method fails
    Input:
    N = 9
    ar [] = { -3 -18 -22 -21 -17 16 -14 28 -22
    }
    Its Correct output is:
    30
    And Your Code's output is:
    8

  • @sundeepkumar3871
    @sundeepkumar3871 3 ปีที่แล้ว +1

    INT_MIN
    I dint understand what to write in c++ code

  • @raj.saurabhh
    @raj.saurabhh 3 ปีที่แล้ว

    3rd one best

  • @shekharkumarsingh370
    @shekharkumarsingh370 ปีที่แล้ว

    3rd method not working [1, -2, 3, -2] correct ans=3 , by erd method ans=2.

    • @shekharkumarsingh370
      @shekharkumarsingh370 ปีที่แล้ว

      at the end we need to take max of normal kandal algorithem and 3rd method answer.

  • @kobo3344
    @kobo3344 4 ปีที่แล้ว

    In method 4, how is the total sum = min sum, if all elements are positive? Can someone please explain this?

    • @eyeamkd
      @eyeamkd 2 ปีที่แล้ว

      Not positive, total_sum = min_sum when all the elements are negative, run a brute force, you'll understand

  • @agnisain1771
    @agnisain1771 3 ปีที่แล้ว +1

    Is your name Satyam Kumar?

    • @techdose4u
      @techdose4u  3 ปีที่แล้ว

      Nope. Follow linkedIn and Instagram to know me 😉

  • @5_tejabvs818
    @5_tejabvs818 ปีที่แล้ว

    isn't the 3rd and 4th methods exactly same..

  • @The_Promised_Neverland...
    @The_Promised_Neverland... 2 ปีที่แล้ว

    SOLUTION:-
    int maxSubarraySumCircular(vector& nums) {
    int mx=*max_element(nums.begin(),nums.end());
    if(mx

  • @hasnainmalick5777
    @hasnainmalick5777 3 ปีที่แล้ว

    your every video lefts many confusions though you dont explain it well

  • @RatulRoy-gc8yj
    @RatulRoy-gc8yj 2 หลายเดือนก่อน

    man why dont u show full approaches just because its difficult to implement like what is wrong with u

  • @umangmalhotra1222
    @umangmalhotra1222 2 ปีที่แล้ว

    kya bakwas and boring video bnayi hai.

  • @ace2825
    @ace2825 2 ปีที่แล้ว

    This person has just horribly messed up the whole solution! If you are showing multiple approaches, please dont pin rudimentary explanation for something not explained in detail

  • @KoushikChowdhury2016
    @KoushikChowdhury2016 4 ปีที่แล้ว

    Here is the C++ code of 3rd Method
    class Solution {
    public:
    int maxSubarraySumCircular(vector& A) {
    int n=A.size();
    int curr_sum=A[0], max_sum=A[0];
    for(int i=1;i

  • @josephhernandez8086
    @josephhernandez8086 ปีที่แล้ว

    Roobot

  • @Ayush37262
    @Ayush37262 2 หลายเดือนก่อน

    Poor explanation!!!

  • @bhargavreddy5938
    @bhargavreddy5938 4 ปีที่แล้ว

    In the second solution of min sub-array sum and total array sum if the input is [5,-3,2,-6,-1,4] the min sub array will be =-7 and total sum will be -1 which is equal to -8, which is wrong. XXXXX.