There are N stacks of coins, numbered from 0 to N−1. The Kth stack has A[K] coins. In one move we can pick two coins from any stack we choose, put the first coin away and place the second coin on the adjacent stack (either to the left or to the right of the original stack).
What is the maximum number of coins that can be accumulated in a single stack?
Write a function:
function solution(A);
that, given an array A of N integers, recording the heights of the stacks, returns the maximum number of coins that can be accumulated in one stack after performing any number of moves as described above.
Examples:
1. Given A = [2, 3, 1, 3], the function should return 5. A possible sequence of moves is: [2, 3, 1, 3] → [0, 4, 1, 3] → [0, 4, 2, 1] → [0, 5, 0, 1].
2. Given A = [3, 7, 0, 5], the function should return 9. A possible sequence of moves is: [3, 7, 0, 5] → [1, 8, 0, 5] → [1, 8, 1, 3] → [1, 8, 2, 1] → [1, 9, 0, 1].
3. Given A = [1, 1, 1, 1, 1], the function should return 1. No move can be performed.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..200,000];
- each element of array A is an integer within the range [0..100,000,000].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
const solution = (a) => {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
};
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
const solution = (a) => {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
const solution = (a) => {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
}
solution.js:26 }(); ^ SyntaxError: Unexpected token ) at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:26 }(); ^ SyntaxError: Unexpected token ) at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
solution.js:26 }(); ^ SyntaxError: Unexpected token ) at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:392:29) at Promise.resolve.then (/tmp/exec.js:426:34) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(a) {
let max = 0;
let arr = [];
let acc = 0;
for(let i = a.length - 1; i > -1; i--) {
arr[i] = a[i] + acc;
acc = (Math.floor((a[i] + acc)/2));
}
acc = 0;
for(let i = 0; i < a.length; i++) {
if(arr[i] + acc > max) {
max = arr[i] + acc;
}
acc = (Math.floor((a[i] + acc)/2));
}
return max;
}
The solution obtained perfect score.