Last active
February 28, 2024 18:18
-
-
Save nirmalrepo/4b45bd4914899f7d31445a69b80bbf96 to your computer and use it in GitHub Desktop.
12 Men Question
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let allPeople = [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]; | |
let groupA = allPeople.slice(0, 4); | |
let groupB = allPeople.slice(4, 8); | |
let groupC = allPeople.slice(8, 12); | |
seeSawOne = weightDifference(groupA, groupB); | |
console.log("seeSawOne", seeSawOne); | |
if (seeSawOne === 0) { | |
normalGroup = groupA; | |
abnormalGroup = groupC; | |
} else if (seeSawOne < 0) { | |
//A-B | |
normalGroup = groupC; | |
// Additional check to find out if GroupA are equal to GroupC | |
abnormalGroup = arrayTotal(groupA) == arrayTotal(groupC) ? groupB : groupA; | |
} else { | |
normalGroup = groupC; | |
abnormalGroup = arrayTotal(groupB) == arrayTotal(groupC) ? groupA : groupB; | |
} | |
seeSawTwo = weightDifference( | |
normalGroup.slice(0, 2), | |
abnormalGroup.slice(0, 2) | |
); | |
console.log("seeSawTwo", seeSawTwo); | |
let normalGroupCountOne = normalGroup.slice(0, 1); | |
if (seeSawTwo === 0) { | |
abnormalGroupCountOne = abnormalGroup.slice(2, 3); | |
abnormalGroupCountTwo = abnormalGroup.slice(3, 4); | |
} else if (seeSawTwo < 0) { | |
abnormalGroupCountOne = abnormalGroup.slice(0, 1); | |
abnormalGroupCountTwo = abnormalGroup.slice(1, 2); | |
} else { | |
abnormalGroupCountOne = abnormalGroup.slice(1, 2); | |
abnormalGroupCountTwo = abnormalGroup.slice(0, 1); | |
} | |
seeSawThree = weightDifference(normalGroupCountOne, abnormalGroupCountOne); | |
console.log("seeSawThree", seeSawThree); | |
if (seeSawThree === 0) { | |
differentPersonWeight = abnormalGroupCountTwo; | |
} else if (seeSawThree < 0) { | |
differentPersonWeight = abnormalGroupCountOne; | |
} else { | |
differentPersonWeight = abnormalGroupCountOne; | |
} | |
console.log("differentPersonWeight", differentPersonWeight); | |
const position = allPeople.indexOf(differentPersonWeight[0]); | |
console.log("differentPersonPosition", position + 1); | |
function arrayTotal(arr) { | |
return arr.reduce((acc, val) => acc + val, 0); | |
} | |
function weightDifference(leftSide, rightSide) { | |
const leftSideWeight = arrayTotal(leftSide); | |
const rightSideWeight = arrayTotal(rightSide); | |
return leftSideWeight - rightSideWeight; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment