This is a demo task.
An array A consisting of N integers is given. An equilibrium index of this array is any integer P such that 0 ≤ P < N and the sum of elements of lower indices is equal to the sum of elements of higher indices, i.e.
A[0] + A[1] + ... + A[P−1] = A[P+1] + ... + A[N−2] + A[N−1].
Sum of zero elements is assumed to be equal to 0. This can happen if P = 0 or if P = N−1.
For example, consider the following array A consisting of N = 8 elements:
A[0] = -1 A[1] = 3 A[2] = -4 A[3] = 5 A[4] = 1 A[5] = -6 A[6] = 2 A[7] = 1P = 1 is an equilibrium index of this array, because:
- A[0] = −1 = A[2] + A[3] + A[4] + A[5] + A[6] + A[7]
P = 3 is an equilibrium index of this array, because:
- A[0] + A[1] + A[2] = −2 = A[4] + A[5] + A[6] + A[7]
P = 7 is also an equilibrium index, because:
- A[0] + A[1] + A[2] + A[3] + A[4] + A[5] + A[6] = 0
and there are no elements with indices greater than 7.
P = 8 is not an equilibrium index, because it does not fulfill the condition 0 ≤ P < N.
Write a function:
int solution(int A[], int N);
that, given an array A consisting of N integers, returns any of its equilibrium indices. The function should return −1 if no equilibrium index exists.
For example, given array A shown above, the function may return 1, 3 or 7, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A) {
// write your code in Objective-C 2.0
}
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < n; i++) {
long long rightSum = sum - leftsum - A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:10:1: warning: control reaches end of non-void function [-Wreturn-type] } ^ ./func.m:12:5: error: redefinition of 'solution' int solution(NSMutableArray *A) ^ ./func.m:8:5: note: previous definition is here int solution(NSMutableArray *A) { ^ ./func.m:27:19: error: use of undeclared identifier 'n' for (i=0; i < n; i++) { ^ ./func.m:28:44: error: invalid operands to binary expression ('long long' and 'id') long long rightSum = sum - leftsum - A[i]; ~~~~~~~~~~~~~ ^ ~~~~ ./func.m:32:17: error: arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform leftsum += A[i]; ^ ~~~~ 1 warning and 4 errors generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < n; i++) {
long long rightSum = sum - leftsum - A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:23:19: error: use of undeclared identifier 'n' for (i=0; i < n; i++) { ^ ./func.m:24:44: error: invalid operands to binary expression ('long long' and 'id') long long rightSum = sum - leftsum - A[i]; ~~~~~~~~~~~~~ ^ ~~~~ ./func.m:28:17: error: arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform leftsum += A[i]; ^ ~~~~ 3 errors generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:24:44: error: invalid operands to binary expression ('long long' and 'id') long long rightSum = sum - leftsum - A[i]; ~~~~~~~~~~~~~ ^ ~~~~ ./func.m:28:17: error: arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform leftsum += A[i]; ^ ~~~~ 2 errors generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:28:17: error: arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform leftsum += A[i]; ^ ~~~~ 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
printf("Came here")
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:12:28: error: expected ';' after expression printf("Came here") ^ ; 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
return -1;
printf("Came here");
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("%d",sum);
printf("%d",leftsum);
printf("%d",rightsum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:26:21: warning: format specifies type 'int' but the argument has type 'long long' [-Wformat] printf("%d",sum); ~~ ^~~ %lld ./func.m:27:21: warning: format specifies type 'int' but the argument has type 'long long' [-Wformat] printf("%d",leftsum); ~~ ^~~~~~~ %lld warning: format specifies type 'int' but the argument has type '<dependent type>' [-Wformat] ./func.m:28:21: error: use of undeclared identifier 'rightsum'; did you mean 'rightSum'? printf("%d",rightsum); ^~~~~~~~ rightSum ./func.m:25:19: note: 'rightSum' declared here long long rightSum = sum - leftsum - (long long)A[i]; ^ 3 warnings and 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("%ld",sum);
printf("%ld",leftsum);
printf("%ld",rightsum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:26:22: warning: format specifies type 'long' but the argument has type 'long long' [-Wformat] printf("%ld",sum); ~~~ ^~~ %lld ./func.m:27:22: warning: format specifies type 'long' but the argument has type 'long long' [-Wformat] printf("%ld",leftsum); ~~~ ^~~~~~~ %lld warning: format specifies type 'long' but the argument has type '<dependent type>' [-Wformat] ./func.m:28:22: error: use of undeclared identifier 'rightsum'; did you mean 'rightSum'? printf("%ld",rightsum); ^~~~~~~~ rightSum ./func.m:25:19: note: 'rightSum' declared here long long rightSum = sum - leftsum - (long long)A[i]; ^ 3 warnings and 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("%lld",sum);
printf("%lld",leftsum);
printf("%lld",rightsum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
warning: format specifies type 'long long' but the argument has type '<dependent type>' [-Wformat] In file included from exec.m:31: ./func.m:28:23: error: use of undeclared identifier 'rightsum'; did you mean 'rightSum'? printf("%lld",rightsum); ^~~~~~~~ rightSum ./func.m:25:19: note: 'rightSum' declared here long long rightSum = sum - leftsum - (long long)A[i]; ^ 1 warning and 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("%lld",sum);
printf("%lld",leftsum);
printf("%lld",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
9429333007857803494293330157152966286235494293330314309766286238594293330314309454714651394293330471468173143102594293330628623053143107294293330628622581571548894293330785778420
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 164728146 Left: 0 Right: 137273714 Sum: 164728146 Left: 27454432 Right: 109818898 Sum: 164728146 Left: 54909248 Right: 109818929 Sum: 164728146 Left: 54909217 Right: 82363921 Sum: 164728146 Left: 82364225 Right: 54909297 Sum: 164728146 Left: 109818849 Right: 54909344 Sum: 164728146 Left: 109818802 Right: 27454624 Sum: 164728146 Left: 137273522 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i = 0;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 17390560 Sum: 34781504 Sum: 34781473 Sum: 52172609 Sum: 69563361 Sum: 69563314 Sum: 86954162 Sum: 104344914 Sum: 104344914 Left: 0 Right: 86954354 Sum: 104344914 Left: 17390560 Right: 69563410 Sum: 104344914 Left: 34781504 Right: 69563441 Sum: 104344914 Left: 34781473 Right: 52172305 Sum: 104344914 Left: 52172609 Right: 34781553 Sum: 104344914 Left: 69563361 Right: 34781600 Sum: 104344914 Left: 69563314 Right: 17390752 Sum: 104344914 Left: 86954162 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 36375520 Sum: 72751424 Sum: 72751393 Sum: 109127489 Sum: 145503201 Sum: 145503154 Sum: 181878962 Sum: 218254674 Sum: 218254674 Left: 0 Right: 181879154 Sum: 218254674 Left: 36375520 Right: 145503250 Sum: 218254674 Left: 72751424 Right: 145503281 Sum: 218254674 Left: 72751393 Right: 109127185 Sum: 218254674 Left: 109127489 Right: 72751473 Sum: 218254674 Left: 145503201 Right: 72751520 Sum: 218254674 Left: 145503154 Right: 36375712 Sum: 218254674 Left: 181878962 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 20536288 Sum: 41072960 Sum: 41072929 Sum: 61609793 Sum: 82146273 Sum: 82146226 Sum: 102682802 Sum: 123219282 Sum: 123219282 Left: 0 Right: 102682994 Sum: 123219282 Left: 20536288 Right: 82146322 Sum: 123219282 Left: 41072960 Right: 82146353 Sum: 123219282 Left: 41072929 Right: 61609489 Sum: 123219282 Left: 61609793 Right: 41073009 Sum: 123219282 Left: 82146273 Right: 41073056 Sum: 123219282 Left: 82146226 Right: 20536480 Sum: 123219282 Left: 102682802 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 38882272 Sum: 77764928 Sum: 77764897 Sum: 116647745 Sum: 155530209 Sum: 155530162 Sum: 194412722 Sum: 233295186 Sum: 233295186 Left: 0 Right: 194412914 Sum: 233295186 Left: 38882272 Right: 155530258 Sum: 233295186 Left: 77764928 Right: 155530289 Sum: 233295186 Left: 77764897 Right: 116647441 Sum: 233295186 Left: 116647745 Right: 77764977 Sum: 233295186 Left: 155530209 Right: 77765024 Sum: 233295186 Left: 155530162 Right: 38882464 Sum: 233295186 Left: 194412722 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
In file included from exec.m:31: ./func.m:19:13: error: arithmetic on pointer to interface 'id', which is not a constant size for this architecture and platform sum += A[i]; ^ ~~~~ 1 error generated.
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 15399904 Sum: 30800192 Sum: 30800161 Sum: 46200641 Sum: 61600737 Sum: 61600690 Sum: 77000882 Sum: 92400978 Sum: 92400978 Left: 0 Right: 77001074 Sum: 92400978 Left: 15399904 Right: 61600786 Sum: 92400978 Left: 30800192 Right: 61600817 Sum: 92400978 Left: 30800161 Right: 46200337 Sum: 92400978 Left: 46200641 Right: 30800241 Sum: 92400978 Left: 61600737 Right: 30800288 Sum: 92400978 Left: 61600690 Right: 15400096 Sum: 92400978 Left: 77000882 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (int)A[i];
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
Sum: 16346080 Sum: 32692544 Sum: 32692513 Sum: 49039169 Sum: 65385441 Sum: 65385394 Sum: 81731762 Sum: 98078034 Sum: 98078034 Left: 0 Right: 81731954 Sum: 98078034 Left: 16346080 Right: 65385490 Sum: 98078034 Left: 32692544 Right: 65385521 Sum: 98078034 Left: 32692513 Right: 49038865 Sum: 98078034 Left: 49039169 Right: 32692593 Sum: 98078034 Left: 65385441 Right: 32692640 Sum: 98078034 Left: 65385394 Right: 16346272 Sum: 98078034 Left: 81731762 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("A[0]: %lld \n",A[i]);
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
A[0]: 22928352 Sum: 22928352 A[0]: 22928736 Sum: 45857088 A[0]: -31 Sum: 45857057 A[0]: 22928928 Sum: 68785985 A[0]: 22928544 Sum: 91714529 A[0]: -47 Sum: 91714482 A[0]: 22928640 Sum: 114643122 A[0]: 22928544 Sum: 137571666 Sum: 137571666 Left: 0 Right: 114643314 Sum: 137571666 Left: 22928352 Right: 91714578 Sum: 137571666 Left: 45857088 Right: 91714609 Sum: 137571666 Left: 45857057 Right: 68785681 Sum: 137571666 Left: 68785985 Right: 45857137 Sum: 137571666 Left: 91714529 Right: 45857184 Sum: 137571666 Left: 91714482 Right: 22928544 Sum: 137571666 Left: 114643122 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
signed long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("A[0]: %lld \n",(long long)A[i]);
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
A[0]: 35625952 Sum: 35625952 A[0]: 35626336 Sum: 71252288 A[0]: -31 Sum: 71252257 A[0]: 35626528 Sum: 106878785 A[0]: 35626144 Sum: 142504929 A[0]: -47 Sum: 142504882 A[0]: 35626240 Sum: 178131122 A[0]: 35626144 Sum: 213757266 Sum: 213757266 Left: 0 Right: 178131314 Sum: 213757266 Left: 35625952 Right: 142504978 Sum: 213757266 Left: 71252288 Right: 142505009 Sum: 213757266 Left: 71252257 Right: 106878481 Sum: 213757266 Left: 106878785 Right: 71252337 Sum: 213757266 Left: 142504929 Right: 71252384 Sum: 213757266 Left: 142504882 Right: 35626144 Sum: 213757266 Left: 178131122 Right: 0
#import <Foundation/Foundation.h>
// you can also use other imports, for example:
// #import <SomeLibrary/SomeFile.h>
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(NSMutableArray *A)
{
if (A.count==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i;
for (i=0; i<A.count; i++) {
sum += (long long)A[i];
printf("A[%d]: %lld \n",i,(long long)A[i]);
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < A.count; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
Test from the task description
got -1, but equilibrium point exists, for example on position 1
A[0]: 21584864 Sum: 21584864 A[1]: 21585248 Sum: 43170112 A[2]: -31 Sum: 43170081 A[3]: 21585440 Sum: 64755521 A[4]: 21585056 Sum: 86340577 A[5]: -47 Sum: 86340530 A[6]: 21585152 Sum: 107925682 A[7]: 21585056 Sum: 129510738 Sum: 129510738 Left: 0 Right: 107925874 Sum: 129510738 Left: 21584864 Right: 86340626 Sum: 129510738 Left: 43170112 Right: 86340657 Sum: 129510738 Left: 43170081 Right: 64755217 Sum: 129510738 Left: 64755521 Right: 43170161 Sum: 129510738 Left: 86340577 Right: 43170208 Sum: 129510738 Left: 86340530 Right: 21585056 Sum: 129510738 Left: 107925682 Right: 0
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 4.8.2)
if (N==0) {
printf("Came here");
return -1;
}
long long sum = 0;
int i;
for (i=0; i<N; i++) {
sum += (long long)A[i];
printf("A[%d]: %lld \n",i,(long long)A[i]);
printf("Sum: %lld \n",sum);
}
long long leftsum = 0;
for (i=0; i < N; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
printf("Sum: %lld \n",sum);
printf("Left: %lld \n",leftsum);
printf("Right: %lld \n",rightSum);
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
A[0]: -1 Sum: -1 A[1]: 3 Sum: 2 A[2]: -4 Sum: -2 A[3]: 5 Sum: 3 A[4]: 1 Sum: 4 A[5]: -6 Sum: -2 A[6]: 2 Sum: 0 A[7]: 1 Sum: 1 Sum: 1 Left: 0 Right: 2 Sum: 1 Left: -1 Right: -1
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 4.8.2)
if (N==0) {
return -1;
}
long long sum = 0;
int i;
for (i=0; i<N; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < N; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 4.8.2)
if (N==0) {
return -1;
}
long long sum = 0;
int i;
for (i=0; i<N; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < N; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// printf("this is a debug message\n");
int solution(int A[], int N) {
// write your code in C99 (gcc 4.8.2)
if (N==0) {
return -1;
}
long long sum = 0;
int i;
for (i=0; i<N; i++) {
sum += (long long)A[i];
}
long long leftsum = 0;
for (i=0; i < N; i++) {
long long rightSum = sum - leftsum - (long long)A[i];
if (rightSum == leftsum) {
return i;
}
leftsum += (long long)A[i];
}
return -1;
}
The solution obtained perfect score.