DEV Community

Discussion on: Daily Challenge #283 - Simple Missing Sum

Collapse
 
soorajsnblaze333 profile image
Sooraj (PS)
const getCombinations = (arr) => {
  let combinations = [];
  for (let i = 1; i < (Math.pow(2, arr.length)); i++) {
    let subsetArray = [];
    for (let j = 0; j < arr.length; j++)
      if (i & Math.pow(2, j)) subsetArray.push(arr[j]);
    let sum = subsetArray.reduce((a, b) => a + b);
    if (!combinations.includes(sum)) combinations.push(sum);
  }
  return combinations;
}

const solve = (arr) => {
  let max = arr.reduce((a, b) => a + b);
  let combinations = getCombinations(arr);
  for (let index = 1; index < max; index++)
    if (!combinations.includes(index)) return index;
}