I am doing this question using 3 for loops by resetting the pointer j and k whenever they exceed the size of the array. I am getting TLE in 1 test case.
#CODE
using namespace std;
int main() {
int Test;
cin >> Test;
while(Test–) {
int n, arr[1000];
cin >> n;
for(int i = 0; i < n; i++) {
cin >> arr[i];
}
int maxSum = INT_MIN;
// i loop
for(int i = 0; i < n; i++) {
int j = i;
// j loop
do{
int sum = 0;
// k loop
int k = i;
while(k != j) {
sum = sum + arr[k];
k++;
if(k == n) {
k = 0;
}
}
sum = sum + arr[k];
if(sum > maxSum) {
maxSum = sum;
}
j++;
if(j == n) {
j = 0;
}
} while(j != i);
}
cout << maxSum << endl;
}
return 0;
}
Also, I was trying to do this question using the cumulative sum approach but I am not getting the correct answer.
Can you please help me out here
Thank you