A non-empty array A consisting of N integers is given. Array A represents numbers on a tape.
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.
For example, consider array A such that:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3We can split this tape in four places:
- P = 1, difference = |3 − 10| = 7
- P = 2, difference = |4 − 9| = 5
- P = 3, difference = |6 − 7| = 1
- P = 4, difference = |10 − 3| = 7
Write a function:
int solution(int A[], int N);
that, given a non-empty array A of N integers, returns the minimal difference that can be achieved.
For example, given:
A[0] = 3 A[1] = 1 A[2] = 2 A[3] = 4 A[4] = 3the function should return 1, as explained above.
Write an efficient algorithm for the following assumptions:
- 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].
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N], sumFromP[N], diff[N] ;
// write your code in C99
for ( i =0 ; i < N ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
sumBackward = sumBackward + A[N-i-1];
sumFromP[i] = sumBackward;
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
In file included from user.c:23:0: func.c: In function 'solution': func.c:10:11: error: 'i' undeclared (first use in this function) func.c:10:11: note: each undeclared identifier is reported only once for each function it appears in func.c:20:13: error: 'positiveVal' undeclared (first use in this function) func.c:28:24: error: 'maxDiff' undeclared (first use in this function) func.c:7:62: warning: variable 'diff' set but not used [-Wunused-but-set-variable] func.c:7:49: warning: variable 'sumFromP' set but not used [-Wunused-but-set-variable] func.c:7:38: warning: variable 'sumToP' set but not used [-Wunused-but-set-variable] func.c:36:1: warning: control reaches end of non-void function [-Wreturn-type]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N], sumFromP[N], diff[N] ;
unsined int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
sumBackward = sumBackward + A[N-i-1];
sumFromP[i] = sumBackward;
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
In file included from user.c:23:0: func.c: In function 'solution': func.c:8:5: error: 'unsined' undeclared (first use in this function) func.c:8:5: note: each undeclared identifier is reported only once for each function it appears in func.c:8:13: error: expected ';' before 'int' func.c:11:11: error: 'i' undeclared (first use in this function) func.c:21:13: error: 'positiveVal' undeclared (first use in this function) func.c:29:24: error: 'maxDiff' undeclared (first use in this function) func.c:7:62: warning: variable 'diff' set but not used [-Wunused-but-set-variable] func.c:7:49: warning: variable 'sumFromP' set but not used [-Wunused-but-set-variable] func.c:7:38: warning: variable 'sumToP' set but not used [-Wunused-but-set-variable] func.c:37:1: warning: control reaches end of non-void function [-Wreturn-type]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N], sumFromP[N], diff[N] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
sumBackward = sumBackward + A[N-i-1];
sumFromP[i] = sumBackward;
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N], sumFromP[N], diff[N] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
sumBackward = sumBackward + A[N-i-1];
sumFromP[i] = sumBackward;
printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
index 0 - sum forward 3 - sum backward 3 index 1 - sum forward 4 - sum backward 7 index 2 - sum forward 6 - sum backward 9 index 3 - sum forward 10 - sum backward 10 index 4 - sum forward 13 - sum backward 13
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N], sumFromP[N], diff[N] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N ; i++) {
sumFromP[i] = sumToP[N-1] - sumToP[i];
printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
index 0 - sum forward 3 - sum backward 10 index 1 - sum forward 4 - sum backward 9 index 2 - sum forward 6 - sum backward 7 index 3 - sum forward 10 - sum backward 3 index 4 - sum forward 13 - sum backward 0
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] - sumToP[i];
printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
index 0 - sum forward 3 - sum backward 7 index 1 - sum forward 4 - sum backward 6 index 2 - sum forward 6 - sum backward 4 index 3 - sum forward 10 - sum backward 0
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
index 0 - sum forward 3 - sum backward 10 index 1 - sum forward 4 - sum backward 9 index 2 - sum forward 6 - sum backward 7 index 3 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, maxDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, maxDiff=0;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N ; i++) {
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, maxDiff=0;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
if (diff[i] > maxDiff) {
maxDiff = diff[i] ;
}
}
return maxDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return maxDiff;
}
In file included from user.c:23:0: func.c: In function 'solution': func.c:40:12: error: 'maxDiff' undeclared (first use in this function) func.c:40:12: note: each undeclared identifier is reported only once for each function it appears in func.c:7:23: warning: unused variable 'sumBackward' [-Wunused-variable] func.c:43:1: warning: control reaches end of non-void function [-Wreturn-type]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[3, 1, 2, 4, 3][-3,-1,-2,-4,-3]
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[-3, -1, -2, -4, -3]
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
function result: 1
WARNING: producing output may seriously slow down your code!stdout:
diff 7 - sum forward -3 - sum backward -10 diff made positive 7 - sum forward -3 - sum backward -10 diff 5 - sum forward -4 - sum backward -9 diff made positive 5 - sum forward -4 - sum backward -9 diff 1 - sum forward -6 - sum backward -7 diff made positive 1 - sum forward -6 - sum backward -7 diff -7 - sum forward -10 - sum backward -3 diff made positive 7 - sum forward -10 - sum backward -3
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[-3, -1, -2, -4, -3, 0]
WARNING: producing output may seriously slow down your code!stdout:
diff -7 - sum forward 3 - sum backward 10 diff made positive 7 - sum forward 3 - sum backward 10 diff -5 - sum forward 4 - sum backward 9 diff made positive 5 - sum forward 4 - sum backward 9 diff -1 - sum forward 6 - sum backward 7 diff made positive 1 - sum forward 6 - sum backward 7 diff 7 - sum forward 10 - sum backward 3 diff made positive 7 - sum forward 10 - sum backward 3
function result: 1
WARNING: producing output may seriously slow down your code!stdout:
diff 7 - sum forward -3 - sum backward -10 diff made positive 7 - sum forward -3 - sum backward -10 diff 5 - sum forward -4 - sum backward -9 diff made positive 5 - sum forward -4 - sum backward -9 diff 1 - sum forward -6 - sum backward -7 diff made positive 1 - sum forward -6 - sum backward -7 diff -7 - sum forward -10 - sum backward -3 diff made positive 7 - sum forward -10 - sum backward -3 diff -13 - sum forward -13 - sum backward 0 diff made positive 13 - sum forward -13 - sum backward 0
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
//printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
//printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[-3, -1, -2, -4, 2, 0]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
//printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
//printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[-3, -1, -2, -4, 2, 5, 6, 0]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
//printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
//printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
[-3, -1, -2, -4, 2, 5, 6, 0]
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// maybe should mallac these arrays but for now assume enough space on
// stack
int sumForward=0, sumBackward=0, sumToP[N-1], sumFromP[N-1], diff[N-1] ;
unsigned int i, positiveVal, minDiff;
// write your code in C99
for ( i =0 ; i < N-1 ; i++) {
sumForward = sumForward + A[i];
sumToP[i] = sumForward;
}
sumForward = 0;
for ( i =0 ; i < N-1 ; i++) {
sumFromP[i] = sumToP[N-2] + A[N-1] - sumToP[i];
// printf("index %d - sum forward %d - sum backward %d \n", i, sumToP[i],sumFromP[i] );
}
for ( i =0 ; i < N-1 ; i++) {
diff[i] = sumToP[i] - sumFromP[i];
//printf("diff %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
if (diff[i] < 0) {
positiveVal = ~diff[i] + 1;
diff [i] = positiveVal;
}
//printf("diff made positive %d - sum forward %d - sum backward %d \n", diff[i], sumToP[i],sumFromP[i] );
}
minDiff = diff[0];
for ( i =1 ; i < N-1 ; i++) {
if (diff[i] < minDiff) {
minDiff = diff[i] ;
}
}
return minDiff;
}
The solution obtained perfect score.