Finding all possible sums in an array - Javascript

preview_player
Показать описание
Short video tutorial on how to find all the possible combinations of sums within a given array.
Рекомендации по теме
Комментарии
Автор

Thank you, I couldn't wrap my head around this shit for days (clearly I'm a noob).
I appreciate the chill, straightforward vibes btw.
Subscribed.

humdough
Автор

I felt like crying when he said: "Well anyway, that was an easy one." I thought it looked pretty difficult. Looks like I still have a little ways to go with programming.

ThePaulKM
Автор

Are you just factoring in pair sums? [1, 4, 5] would also === 10 too yah?

not_enoughmana
Автор

Write a program to find minimum elements in array which has sum equals to given value.

Input :

Array of integers
Required sum
Output :

Elements from array which makes sum equals to given value
Example:

Input Array : [10, 0, -1, 20, 25, 30]
Required Sum: 45
Output: [20, 25]

Required Sum: 59
Output: [10, -1, 20, 30]

Required Sum: 60
Output: [10, 20, 30]

Plz suggest me flow chart for these problems

varshapatil
Автор

for the question, isn't there also the possible answers of 1+2+3+4, 1+2+7, 2+3+5, etc? how would you show that?

showduhtung
Автор

const find = (arr, num) => {
let max = 0, result = [];
arr.forEach( (item, i) => {
let diff = num - item;
if ( max >= diff )
if ( arr.some( (value, ind) => value === diff && ind < i) ) result.push([item, diff]);
if (item > max ) max = item;
});
return result;
}

is this a good solution too?

eduard
Автор

What about more than a pair? Say 5+ 3 + 2

zqlimy
Автор

This should be reworded. What your trying to do is find all pairs that add up to 10, not all possible combinations.

justadev____