pls tell me the problem in the logic
Pls tell the problem in the code
#include #include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t–){ int n; cin>>n; int *arr=new int[n]; int max_so_far=INT_MIN; int sum; if(arr[0]>0){ sum=arr[0]; } else if(arr[0]<0){ sum=0; } int i=1; while(i<n){ sum+=arr[i]; int index=-1; if(sum<0){ sum=0; index=i; } if(i>n-1){ i=0; } if(i==index+1){ break; } max_so_far=max(max_so_far,sum); i++; } cout<<max_so_far<<endl; } return 0; }
bro pls take this code and place it in your ide
bro text dont render properly due to which code produces so many formating and syntax error.
use gfg ide -> https://ide.geeksforgeeks.org/
this is the non error one output it is showing is 0.
u r not readin array elements
still a wrong answer pls check
declare ur index variable outside while loop and then try.
i m not sure whether ur logic will work or not.
refer this for solution->
The maximum subarray sum in circular array is maximum of following
- Maximum subarray sum in non circular array
- Maximum subarray sum in circular array.
Example - [1,2,5,-2,-3,5]
Steps -
- Find the maximum subarray sum using Kadane Algorithm. This is maximum sum for non circular array.
- 1+2+5 = 8
- For non circular sum,
Step 1) find the minimum subarray sum of array.
-2-3 = -5
Step 2) Now find the total sum of array = 1 + 2 + 5 -2 - 3 + 5 = 8
Step 3) The max subarray sum for circular array = Total Sum - Minimum subarray Sum
= 8 - (-5) = 8 + 5 = 13
As illustrated above, substracting minimum subarray sum from total sum gives maximum circular subarray sum.
Answer = Max ( Non circular max sum + circular max sum ) = max(8,13) = 13
you there sir ?? pls reply
chekc my last response
bro u have given me the direct algo to solve thr problem bt i test how will i think it myself i have to approach with normal kadanes
so tell me whether u can crrct my code??
handle ur break condition correclty. rest look correct to me
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.

