A crisis came to Snow White, who was living peacefully with the seven dwarfs to escape the queen. There were nine dwarfs who returned from work instead of seven.
All nine dwarfs claimed to be the protagonists of “Snow White and the Seven Dwarfs.” Snow White, who had great mathematical intuition, remembered that the sum of the heights of the seven dwarfs was 100.
Given the height of the nine dwarfs, write a program to help Snow White find the seven dwarfs.
The heights of the dwarfs are given over nine rows. The given key is a natural number that does not exceed 100, the heights of the nine dwarfs are all different, and if there are several possible answers, any one is output.
20 7 23 19 10 15 25 8 13
The keys of the seven dwarfs are printed in the order they are entered.
20 7 23 19 10 8 13
splice
function solution(arr) {
let answer = arr;
let sum = arr.reduce((a, b) => a + b, 0);
for (let i = 0; i < arr.length - 1; i++) {
for (let j = i + 1; j < arr.length; j++) {
if (sum - (arr[i] + arr[j]) === 100) {
arr.splice(j, 1); //don't start from i, it mess up the index number connection to value
arr.splice(i, 1);
}
}
}
return answer;
}