0

I'm trying to do a simple problem in C that goes like this:

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].

I came up with the following code:

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;
}

Which, according to the site I did this test on, is really bad. The site says the code failed most of the tests they ran, but I can't figure out why (they don't supply the tests). The code seems fine to me. Furthermore, they said that a solution is to be O(n) worst case space complexity (not including input) and I've managed to do it in O(1), so something seems off.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
John H
  • 21
  • 4
  • 1
    You have 2 for loops that iterate over the N elements of an array. That seems like O(n) to me – Manos Nikolaidis Nov 19 '15 at 16:26
  • 1
    "Code Review" in the title? You do know that there's [Code Review](http://codereview.stackexchange.com)? – cadaniluk Nov 19 '15 at 16:27
  • It seems weird.. on the website where this was given I got that the code only completed one test case correctly... – John H Nov 19 '15 at 16:38
  • 1
    @ManosNikolaidis You're referring to time complexity. The website mentions space complexity. –  Nov 19 '15 at 17:07
  • Either I don't understand the problem description, or you haven't told us what `solution` is supposed to calculate ;) –  Nov 19 '15 at 17:07
  • @Rhymoid you are right, just noticed the reference to space complexity. Solving this challenge does require allocating an array. So the site is correct at allowing such a space complexity for the solution – Manos Nikolaidis Nov 19 '15 at 17:15
  • @ManosNikolaidis Without Googling the original problem statement, I'm still not sure what the question is, so I don't know if it is correct. If it's not the maximum difference we're looking for (which would just require O(1) space), the specification for `solution` is not in the question. –  Nov 19 '15 at 17:19

2 Answers2

1

You return the wrong value. They are looking for the minimal difference and not for P of the minimal difference. You can pick a solution from here or here or here in a language of choice.

Community
  • 1
  • 1
Peter Schneider
  • 1,683
  • 12
  • 31
0

Thanks for all the help everyone. I noticed that I didn't update maxDiff, thereby throwing me off. Thanks again!

John H
  • 21
  • 4