C 中的代码审查:我试图解决这个简单谜语的错误在哪里?

Code Review in C: Where is my mistake trying to solve this simple riddle?

我正在尝试用 C 语言做一个简单的问题,如下所示:

Any integer P, such that 0 < P < N, splits this tape into two non-empty parts: A[0], A[1], ..., A[P − 1] and A[P], A[P + 1], ..., A[N − 1]. The difference between the two parts is the value of: |(A[0] + A[1] + ... + A[P − 1]) − (A[P] + A[P + 1] + ... + A[N − 1])| In other words, it is the absolute difference between the sum of the first part and the sum of the second part.

Also: N is an integer within the range [2..100,000]; each element of array A is an integer within the range [−1,000..1,000].

我想出了以下代码:

int solution(int A[], int N) {
// write your code in C99
double firstSum = A[0];
double secondSum = 0;

double curDiff, maxDiff, maxIndex = 1;

for(int i = 1; i < N; i++)
{
    secondSum += A[i];
}

curDiff = abs(firstSum - secondSum);
maxDiff = curDiff;

for(int i = 2; i < N; i++)
{
    secondSum -= A[i-1];
    firstSum += A[i-1];
    curDiff = abs(firstSum - secondSum);
    if(curDiff > maxDiff)
        maxIndex = i;
}
return maxIndex;
}

根据我进行此测试的网站,这真的很糟糕。该网站说代码未能通过他们 运行 的大部分测试,但我不明白为什么(他们不提供测试)。代码对我来说似乎很好。此外,他们说解决方案是 O(n) 最坏情况 space 复杂度(不包括输入),我已经设法在 O(1) 中做到了,所以似乎有些不对劲。

你 return 错误的值。他们正在寻找最小差异而不是最小差异的 P。您可以使用所选语言从 here or here or here 中选择一个解决方案。

感谢大家的帮助。我注意到我没有更新 maxDiff,因此让我失望了。再次感谢!