Maximum circular subarray

question–>
https://practice.geeksforgeeks.org/problems/max-circular-subarray-sum-1587115620/1
code–>


hi i know this one’s complexity is O(n^2) and will not pass all test cases
but i have a doubt
1)how does kadane algorithm work if all elements of the array is negative?,what if most of negative number are at front while calculating with condition added for kadane algorithm to work with negative numbers,how does it work?
negative kadane–>

2)what is the intuition behind it how it works ?

Hey @sheikhhaji18
I am not able to understand what u want to ask
U have shared 2 codes
Why ?

Please be specific

how does kadane works for array with negative elements

Kadane works the same way even if there is a single positive number
In case there might be a case when all nos are negative then we have to tweak it a little
set max_so_far=INT_MIN
and check for max_so_far=max(curr,max_so_far) immediately after updating cur

or u can simply traverse over whole array to check if all negative or not

like this right–>


okay thanks
how to do this question in O(n) time?

Yes exactly

Refer to Maximum circular sum

bro all testcases are not passing

Where are u submitting ?

question–>
[https://practice.geeksforgeeks.org/problems/max-circular-subarray-sum-1587115620/1 ]

Okay i will check :slight_smile:


got it now it is passing all testcases

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.