Create a gist now

Instantly share code, notes, and snippets.

@stanzheng /dojo.js Secret
Created Nov 30, 2016

What would you like to do?
/**
* Code Codo Array Splitting
* Stanley & Jon
* https://www.hackerrank.com/challenges/array-splitting
*/
function processData(input) {
//Enter your code here
let lines = input.split("\n")
let num_tests = parseInt(lines[0]);
for (var i=1; i < lines.length; i+=2) {
console.log(processArray(convertLineToArray(lines[i+1]), parseInt(lines[i])))
}
}
function convertLineToArray(line) {
return line.split(' ').map(digit=>parseInt(digit))
}
function sumArr(arr) {return arr.reduce((a,b)=>a+b);}
function processArray(arr, len) {
//console.log(arr);
if(arr.length <=1){
return 0;
}
let sum = arr.reduce((a,b)=>a+b);
if (sum % 2 !== 0) {
// total of Array should be even, otherwise not possible
return 0;
}
// sort the array
let target = sum/2
let current = 0;
for (let i=0; i < len; i++) {
var elm = arr[i];
current +=elm;
if (current === target) {
var sliceA = arr.slice(0, i + 1);
var sliceB = arr.slice(i + 1)
var a = processArray(sliceA, sliceA.length);
var b = processArray(sliceB, sliceB.length);
return 1 + Math.max(a, b);
}
}
return 0;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment