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(NSMutableArray *A);
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].
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A ) {
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A length]; i++)
{
sumOfAllElements += [[A itemAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (int i = 0; i < [A length]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += [[A itemAtIndex:i] intValue];
rightSide -= [[A itemAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:7: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:7: note: use option -std=c99 or -std=gnu99 to compile your code func.m:7: warning: ‘NSMutableArray’ may not respond to ‘-length’ func.m:7: warning: (Messages without a matching method signature func.m:7: warning: will be assumed to return ‘id’ and accept func.m:7: warning: ‘...’ as arguments.) func.m:7: warning: comparison between pointer and integer func.m:9: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’ func.m:14: error: redefinition of ‘i’ func.m:7: note: previous definition of ‘i’ was here func.m:14: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:14: warning: ‘NSMutableArray’ may not respond to ‘-length’ func.m:14: warning: comparison between pointer and integer func.m:20: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’ func.m:21: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A ) {
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A itemAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (int i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += [[A itemAtIndex:i] intValue];
rightSide -= [[A itemAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:7: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:7: note: use option -std=c99 or -std=gnu99 to compile your code func.m:9: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’ func.m:9: warning: (Messages without a matching method signature func.m:9: warning: will be assumed to return ‘id’ and accept func.m:9: warning: ‘...’ as arguments.) func.m:14: error: redefinition of ‘i’ func.m:7: note: previous definition of ‘i’ was here func.m:14: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:20: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’ func.m:21: warning: ‘NSMutableArray’ may not respond to ‘-itemAtIndex:’
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A ) {
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (int i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += [[A objectAtIndex:i] intValue];
rightSide -= [[A objectAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:7: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:7: note: use option -std=c99 or -std=gnu99 to compile your code func.m:14: error: redefinition of ‘i’ func.m:7: note: previous definition of ‘i’ was here func.m:14: error: ‘for’ loop initial declarations are only allowed in C99 mode
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A ) {
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (int j = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return j;
else
{
leftSide += [[A objectAtIndex:i] intValue];
rightSide -= [[A objectAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:7: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:7: note: use option -std=c99 or -std=gnu99 to compile your code func.m:14: error: ‘for’ loop initial declarations are only allowed in C99 mode
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += [[A objectAtIndex:i] intValue];
rightSide -= [[A objectAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:8: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:8: note: use option -std=c99 or -std=gnu99 to compile your code
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] integerValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += [[A objectAtIndex:i] intValue];
rightSide -= [[A objectAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:8: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:8: note: use option -std=c99 or -std=gnu99 to compile your code
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
for (int i = 0; i < [A count]; i++)
{
sumOfAllElements += (int)[[A objectAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
}
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:8: error: ‘for’ loop initial declarations are only allowed in C99 mode func.m:8: note: use option -std=c99 or -std=gnu99 to compile your code
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += (int)[[A objectAtIndex:i] intValue];
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
}
}
return -1;
}
Test from the task description
got 0, but it is not equilibrium point, left sum (empty set)=0, sum[1..6]=7
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
}
NSLog(@"sumOfAllElements = %d", sumOfAllElements);
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
}
}
return -1;
}
Test from the task description
got 0, but it is not equilibrium point, left sum (empty set)=0, sum[1..6]=7
2012-12-19 05:48:41.104 user.e[2] sumOfAllElements = 0
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
if (leftSide == rightSide)
return i;
else
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
}
}
return -1;
}
Test from the task description
got 0, but it is not equilibrium point, left sum (empty set)=0, sum[1..6]=7
2012-12-19 05:49:21.854 user.e[2] sumOfAllElements = -7 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = -6 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = -1 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = 1 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = -3 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = 0 2012-12-19 05:49:21.860 user.e[2] sumOfAllElements = 0
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
if (leftSide == rightSide)
return i;
}
return -1;
}
Test from the task description
got 5, but it is not equilibrium point, sum[0..4]=-3, sum[6..6]=0
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
leftSide += (int)[[A objectAtIndex:i] intValue];
rightSide -= (int)[[A objectAtIndex:i] intValue];
NSLog(@"leftSide = %d and rightSide = %d", leftSide, rightSide);
if (leftSide == rightSide)
return i;
}
return -1;
}
Test from the task description
got 5, but it is not equilibrium point, sum[0..4]=-3, sum[6..6]=0
2012-12-19 05:53:09.465 user.e[2] leftSide = -7 and rightSide = 7 2012-12-19 05:53:09.471 user.e[2] leftSide = -6 and rightSide = 6 2012-12-19 05:53:09.471 user.e[2] leftSide = -1 and rightSide = 1 2012-12-19 05:53:09.471 user.e[2] leftSide = 1 and rightSide = -1 2012-12-19 05:53:09.471 user.e[2] leftSide = -3 and rightSide = 3 2012-12-19 05:53:09.471 user.e[2] leftSide = 0 and rightSide = 0
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger equiValue;
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
equiValue = (int)[[A objectAtIndex:i] intValue];
rightSide -= equiValue;
NSLog(@"leftSide = %d and rightSide = %d", leftSide, rightSide);
if (leftSide == rightSide)
return i;
leftSide += equivalue;
}
return -1;
}
In file included from user.m:30: func.m: In function ‘equi’: func.m:29: error: ‘equivalue’ undeclared (first use in this function) func.m:29: error: (Each undeclared identifier is reported only once func.m:29: error: for each function it appears in.)
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger equiValue;
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
equiValue = (int)[[A objectAtIndex:i] intValue];
rightSide -= equiValue;
NSLog(@"leftSide = %d and rightSide = %d", leftSide, rightSide);
if (leftSide == rightSide)
return i;
leftSide += equiValue;
}
return -1;
}
WARNING: producing output may seriously slow down your code! 2012-12-19 05:59:09.913 user.e[2] leftSide = 0 and rightSide = 7 2012-12-19 05:59:09.918 user.e[2] leftSide = -7 and rightSide = 6 2012-12-19 05:59:09.919 user.e[2] leftSide = -6 and rightSide = 1 2012-12-19 05:59:09.919 user.e[2] leftSide = -1 and rightSide = -1
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger equiValue;
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
equiValue = (int)[[A objectAtIndex:i] intValue];
rightSide -= equiValue;
if (leftSide == rightSide)
return i;
leftSide += equiValue;
}
return -1;
}
// you can also use imports, for example:
// #import <Foundation/NSDictionary.h>
int equi ( NSMutableArray *A )
{
NSInteger sumOfAllElements = 0;
int i;
for (i = 0; i < [A count]; i++)
{
sumOfAllElements += [[A objectAtIndex:i] intValue];
//NSLog(@"sumOfAllElements = %d", sumOfAllElements);
}
NSInteger equiValue;
NSInteger leftSide = 0;
NSInteger rightSide = sumOfAllElements;
for (i = 0; i < [A count]; i++)
{
equiValue = (int)[[A objectAtIndex:i] intValue];
rightSide -= equiValue;
if (leftSide == rightSide)
return i;
leftSide += equiValue;
}
return -1;
}
The following issues have been detected: wrong answers.
Sequence with extremly large numbers testing arithmetic overflow.
got 2, but it is not equilibrium point, sum[0..1]=4294967294, sum[3..3]=-2
got 0, but it is not equilibrium point, left sum (empty set)=0, sum[1..2]=-4294967296