Happy hacking!
Saw an interesting whiteboard problem from Improving yesterday. Check if a given array is balanced where balanced means that there exists a position in which the sum of all numbers on the left is equal to the sum of all the elements on the right. For example: [1, 2, 3, 3, 2, 1] is balanced [1, 2, 3, 4, 5] is not balanced I don't remember if they dealt with odd arrays such as: [1, 2, 3, 2, 1] might be balanced? Assuming the above is unbalanced, here's one O(n) brute-force solution. let arr1 = [1, 2, 3, 4, 5]; let arr2 = [1, 2, 3, 3, 2, 1]; function checkBalance(arr) { // Balanced if size is 0 or 1 if (arr.length < 2) { return true; } // Balanced if both values are equal if (arr.length == 2) { if (arr[0] == arr[1]) { return true; } } for (let i = 1; i < arr.length; i ++) { let leftArr = arr.slice(0, i); let rightArr = arr.slice(i, arr.length); let leftSum = leftArr.reduce((a, b) => {return a + b}); let rightSum = rightArr.reduce((a, b) => {return a + b}); if (leftSum == rightSum) { return true; } } return false; } console.log(checkBalance(arr1)); console.log(checkBalance(arr2));
A community for programmers (or non-programmers) to come together and let out their inner nerd. if (user.type === "programmer" || user.type === "normie") { console.log(`Welcome, ${user.username}!`); }