sir pls tell me why it’s not working ?? unable to find out the reason and what is the logic required to solve the problem
Max circular sum
hi @rabimajumder08
This Question can be done using the brute force viz, by using two nested loops and checking the sum of every possible subset but this approach is in O(n^2). So, Now we are going to discuss an approach which solves the given problem in O(n).
Approach :
For finding the Maximum Contiguous sum we are using the kadane’s algorithm. But in the question the array is circular that means the maximum sum can be of elements which are a part of the wrapping or not. So,
There can be two cases for the maximum sum:
Case 1: The elements that contribute to the maximum sum are arranged such that no wrapping is there. Examples: {-10, 2, -1, 5}, {-2, 4, -1, 4, -1}. In this case, Kadane’s algorithm will produce the result.
Case 2: The elements which contribute to the maximum sum are arranged such that wrapping is there. Examples: {10, -12, 11}, {12, -5, 4, -8, 11}. In this case, we change wrapping to non-wrapping. Let us see how. Wrapping of contributing elements implies non wrapping of non contributing elements, so find out the sum of non contributing elements and subtract this sum from the total sum. To find out the sum of non contributing, invert sign of each element and then run Kadane’s algorithm. Our array is like a ring and we have to eliminate the maximum continuous negative that implies maximum continuous positive in the inverted arrays.
refer this code -->
unable to understand the wrapping part… sir explain in lucid terms plss
go through this… its difficult to explain the concept through chat…
sir in my code i have used index%n for wrapping and it is showing 22 for the input given in question but not running for test cases…whats wrong in my code pls go through it and tell me
consider the test case
1
5
1 2 3 4 5
expected o/p --> 15
your o/p --> 25
so i cant understand what u are exactly trying to do… neither is it a brute force way, nor the optimized approach… the optimized soln is not that simple as u are thinking…
sir as it is a circular array so i am first traversing to index 0 to n-1 first and then to go to index zero again i am using index%n and i am traversing to n-2 this time(as it is given circular)…and over this i’m using kadane’s algo for the whole(as we do in linear array)
yes, so consider the test case i sent above… the approach is not correct for all test cases… either do a brute force method, or check out the optimized approach…
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.