const sumNum = (n) => {
const arr = n.toString().split('').map(el => +el)
return arr.reduce((acc,c) => acc+c, 0)
}
const isEqual = (n) => {
n = n.toString().split('').map(el => +el)
for(var i = 0; i < n.length; i++) {
if(n[i] > n[i+1]) return false
}
return true
}
function findAll(n, k) {
const res = []
for(let i = 0; i < Number('1'+'0'.repeat(k)); i++) {
if(i.toString().length === k) {
const equal = isEqual(i)
if(equal) {
const val = sumNum(i)
if(n === val && equal) res.push(i)
}
}
}
const maxVal = Math.max(...res)
const minVal = Math.min(...res)
if(res.length === 0) return []
return [res.length, String(minVal), String(maxVal)]
}
function find(sum, n, m) {
if (n === 1) {
return [`${sum}`];
}
const result = [];
const min = Math.max(m, Math.floor(sum - (n - 1) * 9));
const max = Math.floor(sum / n);
for (let i = min; i <= max; i += 1) {
find(sum - i, n - 1, i).forEach((el) => result.push(`${i}${el}`));
}
return result;
}
function findAll(sum, n) {
if (sum > n * 9 || sum < n) {
return [];
}
const result = find(sum, n, 1);
return [result.length, result[0], result.pop()];
}
const isEqual = (n) => {
n = n.toString().split('').map(el => +el)
for(var i = 0; i < n.length; i++) {
if(n[i] > n[i+1]) return false
}
return true
}
function findAll(n, k) {
const res = []
for(let i = Number('1'+'0'.repeat(k-1)); i < Number('1'+'0'.repeat(k)); i++) {
const equal = isEqual(i)
if(equal) {
const val = i.toString().split('').map(el => +el).reduce((acc,c) => acc+c, 0)
if(n === val && equal) res.push(i)
}
}
if(res.length === 0) return []
return [res.length, String(res[0]), String(res[res.length-1])]
}