A non-empty array A consisting of N integers is given. A pair of integers (P, Q), such that 0 ≤ P < Q < N, is called a slice of array A (notice that the slice contains at least two elements). The average of a slice (P, Q) is the sum of A[P] + A[P + 1] + ... + A[Q] divided by the length of the slice. To be precise, the average equals (A[P] + A[P + 1] + ... + A[Q]) / (Q − P + 1).
For example, array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8contains the following example slices:
- slice (1, 2), whose average is (2 + 2) / 2 = 2;
- slice (3, 4), whose average is (5 + 1) / 2 = 3;
- slice (1, 4), whose average is (2 + 2 + 5 + 1) / 4 = 2.5.
The goal is to find the starting position of a slice whose average is minimal.
Write a function:
function solution(A);
that, given a non-empty array A consisting of N integers, returns the starting position of the slice with the minimal average. If there is more than one slice with a minimal average, you should return the smallest starting position of such a slice.
For example, given array A such that:
A[0] = 4 A[1] = 2 A[2] = 2 A[3] = 5 A[4] = 1 A[5] = 5 A[6] = 8the 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 [−10,000..10,000].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j).reduce(function (a,b) { return a+b }) / (j - i)
if ( m === null || c < m ) {
m = c
n = i
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c < m ) {
m = c
n = i
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c < m ) {
m = c
n = i
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n = null
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c <= m ) {
m = c
if ( n === null || i < n ) {
n = i
}
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n = null
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c <= m ) {
m = c
if ( n === null || i < n ) {
n = i
}
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n = null
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
console.log({i, j, c, m, n})
if ( m === null || c <= m ) {
m = c
if ( n === null || i < n ) {
n = i
}
}
}
}
return n
}
{ i: 0, j: 1, c: 3, m: null, n: null } { i: 0, j: 2, c: 2.6666666666666665, m: 3, n: 0 } { i: 0, j: 3, c: 3.25, m: 2.6666666666666665, n: 0 } { i: 0, j: 4, c: 2.8, m: 2.6666666666666665, n: 0 } { i: 0, j: 5, c: 3.1666666666666665, m: 2.6666666666666665, n: 0 } { i: 0, j: 6, c: 3.857142857142857, m: 2.6666666666666665, n: 0 } { i: 0, j: 7, c: 3.375, m: 2.6666666666666665, n: 0 } { i: 1, j: 2, c: 2, m: 2.6666666666666665, n: 0 } { i: 1, j: 3, c: 3, m: 2, n: 0 } { i: 1, j: 4, c: 2.5, m: 2, n: 0 } { i: 1, j: 5, c: 3, m: 2, n: 0 } { i: 1, j: 6, c: 3.8333333333333335, m: 2, n: 0 } { i: 1, j: 7, c: 3.2857142857142856, m: 2, n: 0 } { i: 2, j: 3, c: 3.5, m: 2, n: 0 } { i: 2, j: 4, c: 2.6666666666666665, m: 2, n: 0 } { i: 2, j: 5, c: 3.25, m: 2, n: 0 } { i: 2, j: 6, c: 4.2, m: 2, n: 0 } { i: 2, j: 7, c: 3.5, m: 2, n: 0 } { i: 3, j: 4, c: 3, m: 2, n: 0 } { i: 3, j: 5, c: 3.6666666666666665, m: 2, n: 0 } { i: 3, j: 6, c: 4.75, m: 2, n: 0 } { i: 3, j: 7, c: 3.8, m: 2, n: 0 } { i: 4, j: 5, c: 3, m: 2, n: 0 } { i: 4, j: 6, c: 4.666666666666667, m: 2, n: 0 } { i: 4, j: 7, c: 3.5, m: 2, n: 0 } { i: 5, j: 6, c: 6.5, m: 2, n: 0 } { i: 5, j: 7, c: 4.333333333333333, m: 2, n: 0 } { i: 6, j: 7, c: 4, m: 2, n: 0 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
console.log({i, j, c, m, n})
if ( m === null || c <= m ) {
m = c
if ( c < m || i < n ) {
n = i
}
}
}
}
return n
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ i: 0, j: 1, c: 3, m: null, n: undefined } { i: 0, j: 2, c: 2.6666666666666665, m: 3, n: undefined } { i: 0, j: 3, c: 3.25, m: 2.6666666666666665, n: undefined } { i: 0, j: 4, c: 2.8, m: 2.6666666666666665, n: undefined } { i: 0, j: 5, c: 3.1666666666666665, m: 2.6666666666666665, n: undefined } { i: 0, j: 6, c: 3.857142857142857, m: 2.6666666666666665, n: undefined } { i: 0, j: 7, c: 3.375, m: 2.6666666666666665, n: undefined } { i: 1, j: 2, c: 2, m: 2.6666666666666665, n: undefined } { i: 1, j: 3, c: 3, m: 2, n: undefined } { i: 1, j: 4, c: 2.5, m: 2, n: undefined } { i: 1, j: 5, c: 3, m: 2, n: undefined } { i: 1, j: 6, c: 3.8333333333333335, m: 2, n: undefined } { i: 1, j: 7, c: 3.2857142857142856, m: 2, n: undefined } { i: 2, j: 3, c: 3.5, m: 2, n: undefined } { i: 2, j: 4, c: 2.6666666666666665, m: 2, n: undefined } { i: 2, j: 5, c: 3.25, m: 2, n: undefined } { i: 2, j: 6, c: 4.2, m: 2, n: undefined } { i: 2, j: 7, c: 3.5, m: 2, n: undefined } { i: 3, j: 4, c: 3, m: 2, n: undefined } { i: 3, j: 5, c: 3.6666666666666665, m: 2, n: undefined } { i: 3, j: 6, c: 4.75, m: 2, n: undefined } { i: 3, j: 7, c: 3.8, m: 2, n: undefined } { i: 4, j: 5, c: 3, m: 2, n: undefined } { i: 4, j: 6, c: 4.666666666666667, m: 2, n: undefined } { i: 4, j: 7, c: 3.5, m: 2, n: undefined } { i: 5, j: 6, c: 6.5, m: 2, n: undefined } { i: 5, j: 7, c: 4.333333333333333, m: 2, n: undefined } { i: 6, j: 7, c: 4, m: 2, n: undefined }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
console.log({i, j, c, m, n})
if ( m === null || c < m ) {
m = c
n = i
}
else if ( c == m && i < n ) {
n = i
}
}
}
return n
}
{ i: 0, j: 1, c: 3, m: null, n: undefined } { i: 0, j: 2, c: 2.6666666666666665, m: 3, n: 0 } { i: 0, j: 3, c: 3.25, m: 2.6666666666666665, n: 0 } { i: 0, j: 4, c: 2.8, m: 2.6666666666666665, n: 0 } { i: 0, j: 5, c: 3.1666666666666665, m: 2.6666666666666665, n: 0 } { i: 0, j: 6, c: 3.857142857142857, m: 2.6666666666666665, n: 0 } { i: 0, j: 7, c: 3.375, m: 2.6666666666666665, n: 0 } { i: 1, j: 2, c: 2, m: 2.6666666666666665, n: 0 } { i: 1, j: 3, c: 3, m: 2, n: 1 } { i: 1, j: 4, c: 2.5, m: 2, n: 1 } { i: 1, j: 5, c: 3, m: 2, n: 1 } { i: 1, j: 6, c: 3.8333333333333335, m: 2, n: 1 } { i: 1, j: 7, c: 3.2857142857142856, m: 2, n: 1 } { i: 2, j: 3, c: 3.5, m: 2, n: 1 } { i: 2, j: 4, c: 2.6666666666666665, m: 2, n: 1 } { i: 2, j: 5, c: 3.25, m: 2, n: 1 } { i: 2, j: 6, c: 4.2, m: 2, n: 1 } { i: 2, j: 7, c: 3.5, m: 2, n: 1 } { i: 3, j: 4, c: 3, m: 2, n: 1 } { i: 3, j: 5, c: 3.6666666666666665, m: 2, n: 1 } { i: 3, j: 6, c: 4.75, m: 2, n: 1 } { i: 3, j: 7, c: 3.8, m: 2, n: 1 } { i: 4, j: 5, c: 3, m: 2, n: 1 } { i: 4, j: 6, c: 4.666666666666667, m: 2, n: 1 } { i: 4, j: 7, c: 3.5, m: 2, n: 1 } { i: 5, j: 6, c: 6.5, m: 2, n: 1 } { i: 5, j: 7, c: 4.333333333333333, m: 2, n: 1 } { i: 6, j: 7, c: 4, m: 2, n: 1 }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c < m ) {
m = c
n = i
}
else if ( c == m && i < n ) {
n = i
}
}
}
return n
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
var m = null, c, n
for ( var i = 0; i < A.length; ++i ) {
for ( var j = i + 1; j <= A.length; ++j ) {
c = A.slice(i, j + 1).reduce(function (a,b) { return a+b }) / (j - i + 1)
if ( m === null || c < m ) {
m = c
n = i
}
else if ( c == m && i < n ) {
n = i
}
}
}
return n
}
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [10000, -10000] the solution returned a wrong answer (got 1 expected 0).
increasing, decreasing (legth = ~100) and small functional
got 101 expected 100
numbers from -1 to 1, N = ~100,000
running time: >6.00 sec., time limit: 0.73 sec.
all maximal values, N = ~100,000
running time: >6.00 sec., time limit: 0.76 sec.
many seqeneces, N = ~100,000
running time: >6.00 sec., time limit: 0.70 sec.