[알고리즘] 일곱난쟁이

soheeoott·2021년 5월 21일
0

알고리즘

목록 보기
8/9
post-thumbnail
function solution(arr){
  let answer = arr;
  let sum = 0;

  arr.forEach((el) => {
    sum += el;
  });

  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);
        arr.splice(i, 1);
      };
    }
  }

  answer.sort(function(a, b)  {
    return a - b;
  });
  return answer;
}
let arr = [20, 7, 23, 19, 10, 15, 25, 8, 13];
console.log(solution(arr));
profile
📚 글쓰는 습관 들이기 📚

0개의 댓글