IS
Size: a a a
IS
К
Д
К
IS
IS
IS
ВА
VK
VK
ИД
ИД
ИД
IS
const getCombos = (arr, len) => {самая наивная и элементарная реализация, первая функция из инета, как и обещал, а вторая в подарок от меня
const base = arr.length
const counter = Array(len).fill(base === 1 ? arr[0] : 0)
if (base === 1) return [counter]
const combos = []
const increment = i => {
if (counter[i] === base - 1) {
counter[i] = 0
increment(i - 1)
} else {
counter[i]++
}
}
for (let i = base ** len; i--;) {
const combo = []
for (let j = 0; j < counter.length; j++) {
combo.push(arr[counter[j]])
}
combos.push(combo)
increment(counter.length - 1)
}
return combos
}
const chooseBestDistance=(s,l,sr)=>sr.length<l?null:getCombos(sr,l).reduce((b,cm)=>(c=cm.reduce((a,b)=>a+b),Math.abs(s-c)<Math.abs(s-b)?c:b),0);
console.log(chooseBestDistance(174, 3, [51, 56, 58, 59, 61]));
console.log(chooseBestDistance(163, 3, [50]));
K🦋
IS
МЛ
ВА