Skip to content

Instantly share code, notes, and snippets.

@koji
Last active February 21, 2020 00:38
Show Gist options
  • Save koji/2f857b4d3c66ad85737445142c527ecf to your computer and use it in GitHub Desktop.
Save koji/2f857b4d3c66ad85737445142c527ecf to your computer and use it in GitHub Desktop.
codility
function solution(A) {
  let result = 0;

  for (let element of A) {
    result ^= element
  }

  return result
}
function solution(A) {
  let left = 0;
  let right = 0;
  const arr = [];
  const sum = A.reduce((a, b) => a + b);

  for (let i=0; i<A.length; i++) {
    left += A[i];
    right = sum -left;
    arr.push(Math.abs(left-right));
  }

  return Math.min(...arr);

}

const input = [3,1,2,4,3];

console.log(solution(input));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment