#include <climits>
#include <iostream>
int main() {
int t = 0;
long long int N = 0, currentSum = 0, maxSum = 0, maxSumArray[100] = {0},
A[100][1000] = {0}, smallest = INT_MAX, index_smallest = 0;
std::cin >> N;
for (int i = 0; i < N; ++i) {
std::cin >> t;
for (int j = 0; j < t; ++j) {
std::cin >> A[i][j];
if (smallest > A[i][j]) {
smallest = A[i][j];
index_smallest = j;
}
}
for (int j = index_smallest + 1; j < t; ++j) {
if (A[i][j] > 0 || currentSum != 0) {
currentSum += A[i][j];
if (currentSum >= maxSum) maxSum = currentSum;
if (currentSum < 0) break;
}
}
for (int j = 0; j < index_smallest; ++j) {
if (A[i][j] > 0 || currentSum != 0) {
currentSum += A[i][j];
if (currentSum >= maxSum) maxSum = currentSum;
if (currentSum < 0) break;
}
}
maxSumArray[i] = maxSum;
}
for (int i = 0; i < N; ++i) std::cout << maxSumArray[i] << '\n';
return 0;
}
Please tell me what's wrong in this code?
Hello @moon you are doing the same mistake here also :
i am not getting why you are doing in the 2d array format.
you have to do in the 1d array format:
using the kadane’s algorithm as well:
Here for your reference i am attaching the code:
https://ide.codingblocks.com/s/395421
Happy Learning!!
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.