Why is my approach not giving right solution
@Harshit-Jha-2364832040305354
the approach you are using requires you to maintain a strict window of size n which you are not doing, for example in
1
4
1 2 3 4
answer should be 10, but your answer is 20 because you are considering 8 elements by not maintaining a window of size 4.
So,i can’t manipulate something in my code so that it would return right answer.If possible please give some hint.
If you want to proceed with this approach, after duplicating the array lets say the array is of size 2*n . Now you can use sliding approach with prefix sums.
do let me know where should i ammend in code
Hello @Harshit-Jha-2364832040305354, see the logic must be like
first of all find the maximum sum of the subarray using kadane
and we need to maximize in terms of the circular array.
so suppose from some nos.
a1 a2 a3 a4 … an-1 an you have chosen some segment
a1 a2 a3 [ a4 a5 a6 … an-4 an-3 ] an-2 an-1 an just for an example a4 to an-3 is the subarray with the maximum sum.
Now calculate the total sum of this array and also make every element negative of that. So it will be like ar = -ar
Now if we find out the maximum subarray using kadane’s and let’s mark it as max_negsum
now negative value of this will be the minimum subarray of the original given array and now return the max of the totalsum-max_negsum,orginal ans using kadane.
This approach is absolutely right.
And take the separate case if all the elements are negative then just return the maximum element.
I hope this apporach is clear to you in case there is some doubt pls let me know.
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.