# Working Code in C, any improvements?

#1

All test cases are passing O(N),

int* maxset(int* A, int n1, int *len1) {
int i = 0, flag = 0, minus = 0;
unsigned long long sum = 0, result = 0;
int index = 0, base = 0, last = 0;

``````for (i = 0; i < n1; i++) {
if (A[i] >= 0) {
if (!flag) {
index = i;
flag = 1;
minus = 1;
}
sum += A[i];
} else {
if (sum > result) {
result = sum;
base = index;
last = i - 1;
} else if (result == sum) {
if ((last - base) < ((i-1) - index)) {
base = index;
last = i - 1;
} else if (((last - base) == ((i-1)- index)) && (base <= index)) {
base = index;
last = i - 1;
}
}
flag = 0;
sum = 0;
}
}

*len1 = 0;
if (!minus)
return NULL;

if (sum > result) {
result = sum;
base = index;
last = n1 - 1;
} else if (result == sum) {
if ((last - base) < ((i-1) - index)) {
base = index;
last = i - 1;
} else if (((last - base) == ((i-1) - index)) && (base <= index)) {
base = index;
last = i - 1;
}
}

*len1 = (last - base) + 1;
int *array = (int *) malloc (*len1 * sizeof(int));
for (i = 0; i < *len1; i++) {
array[i] = A[base + i];
}

return array;
``````

}