**Task Score**

**Correctness**

**Performance**

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] = 1`

P = 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:

function solution(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].

*not defined yet*

**Failed**

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < len; i++){
console.log("i: " + i);
}
return arrLen;
}
```

**Example tests**

**RUNTIME ERROR**, tested program terminated unexpectedly

/tmp/solution.js:12 for(i = 0; i < len; i++){ ^ ReferenceError: len is not defined at solution (/tmp/solution.js:12:20) at run (/tmp/user.js:203:22) at main (/tmp/user.js:222:5) at Object.<anonymous> (/tmp/user.js:225:1) at Module._compile (module.js:434:26) at Object.Module._extensions..js (module.js:452:10) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at Function.Module.runMain (module.js:475:10) at startup (node.js:117:18)

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < arrLen; i++){
console.log("i: " + i);
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

i: 0 i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
var firstSum = 0;
var secSum = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var currIndex = i;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
console.log("firstChink: " + j);
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j = 0; j < lenJ; j++){
console.log("secChunk: " + j);
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
var firstSum = 0;
var secSum = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
console.log("firstChink: " + j);
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j = 0; j < lenJ; j++){
console.log("secChunk: " + j);
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

firstChink: 0 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 7 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
var firstSum = 0;
var secSum = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
console.log("firstChink: " + j);
}
//get second chunk
j = currIndex + 2;
lenJ = arrLen;
for(j = 0; j < lenJ; j++){
console.log("secChunk: " + j);
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

firstChink: 0 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 7 secChunk: 0 secChunk: 1 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
var firstSum = 0;
var secSum = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
console.log("firstChink: " + j);
}
//get second chunk
j = currIndex + 2;
lenJ = arrLen;
for(j; j < lenJ; j++){
console.log("secChunk: " + j);
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

firstChink: 0 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
var firstSum = 0;
var secSum = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
console.log("firstChink: " + j);
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j; j < lenJ; j++){
console.log("secChunk: " + j);
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

firstChink: 0 secChunk: 2 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 secChunk: 3 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 secChunk: 4 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 secChunk: 5 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 secChunk: 6 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 secChunk: 7 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 0 firstChink: 1 firstChink: 2 firstChink: 3 firstChink: 4 firstChink: 5 firstChink: 6 firstChink: 7

**Failed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var firstSum = 0;
var secSum = 0;
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
//console.log("firstChink: " + j);
firstSum += A[j];
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j; j < lenJ; j++){
//console.log("secChunk: " + j);
secSum += A[j];
}
}
return arrLen;
}
```

**Example tests**

**WRONG ANSWER**, got 8, which is not valid array index

**Passed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var firstSum = 0;
var secSum = 0;
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
//console.log("firstChink: " + j);
firstSum += A[j];
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j; j < lenJ; j++){
//console.log("secChunk: " + j);
secSum += A[j];
}
if(firstSum == secSum){
return currIndex;
}
}
return arrLen;
}
```

**Passed**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var firstSum = 0;
var secSum = 0;
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
//console.log("firstChink: " + j);
firstSum += A[j];
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j; j < lenJ; j++){
//console.log("secChunk: " + j);
secSum += A[j];
}
if(firstSum == secSum){
return currIndex;
}
}
return arrLen;
}
```

**23**

```
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 4.0.0
var arrLen = A.length;
var i = 0;
for(i = 0; i < arrLen; i++){
//console.log("i: " + i);
var firstSum = 0;
var secSum = 0;
var currIndex = i + 1;
//get first chunk
var j = 0;
var lenJ = currIndex;
for(j = 0; j < lenJ; j++){
//console.log("firstChink: " + j);
firstSum += A[j];
}
//get second chunk
j = currIndex + 1;
lenJ = arrLen;
for(j; j < lenJ; j++){
//console.log("secChunk: " + j);
secSum += A[j];
}
if(firstSum == secSum){
return currIndex;
}
}
return arrLen;
}
```

The following issues have been detected: wrong answers, timeout errors.

For example, for the input `[]`
the solution
returned a wrong answer (got 0, which is not valid array index).

**Correctness tests**

**WRONG ANSWER**, got 6, which is not valid array index

**OK**

Sequence with extremely large numbers testing arithmetic overflow.

**WRONG ANSWER**

got 4, which is not valid array index

**WRONG ANSWER**, got 4, which is not valid array index

**OK**

Sequence with extremely large numbers testing arithmetic overflow.

**WRONG ANSWER**

got 1, which is not valid array index

**WRONG ANSWER**, got 1, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

one large number at the end of the sequence

**WRONG ANSWER**

got 5, which is not valid array index

**WRONG ANSWER**, got 5, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 1, which is not valid array index

**WRONG ANSWER**, got 1, which is not valid array index

**WRONG ANSWER**, got 0, which is not valid array index

multiple runs, all pairs of values: -1, 0 and 1

**WRONG ANSWER**

got 2, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

**OK**

**OK**

**OK**

**WRONG ANSWER**, got 2, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

**WRONG ANSWER**, got 2, which is not valid array index

multiple runs, all triples of values -1, 0 and 1

**WRONG ANSWER**

got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**WRONG ANSWER**, got 3, which is not valid array index

**WRONG ANSWER**, got 3, which is not valid array index

**OK**

**OK**

**Correctness/performance tests**

**TIMEOUT ERROR**, running time: 5.40 sec., time limit: 0.88 sec.

**Performance tests**

**TIMEOUT ERROR**, running time: >6.00 sec., time limit: 0.87 sec.

**TIMEOUT ERROR**, running time: >6.00 sec., time limit: 0.90 sec.

**TIMEOUT ERROR**, running time: >6.00 sec., time limit: 0.91 sec.

**OK**

**OK**

**OK**