Find m numbers that sum to n in a sequence

Jorge Zapata

Given a sequence from 1 to n, I want to find all the unique subsequences of size m that sum up to n. The subsequences don't need to be contiguous. For example

if m = 2, n = 5
The possible unique subsequences of size 2 are {1,4} and {2,3}

So far I've been able to generate all the subsequences using recursion but my code is not returning only the unique subsequences, there are some duplicates in the result.

function allCombinations(m, n) {
if (m < 1) {
    return [];
}

let helper = function(m, n, arr, result, index){
    if (n < 0 || m < 0) {
        return 0;
    }

    if (m === 0 && n === 0) {
        result.push(arr.slice());
        return 1;
    }

    for (let i = index; i <= n; i++){
        arr.push(i);
        helper(m - 1, n - i, arr, result, index + 1);
        arr.pop();
    }

}

let result = [];
helper(m, n, [], result, 0);

return result;
}

and when calling with

let res = allCombinations(2, 5);

the result is

[ [ 1, 4 ], [ 2, 3 ], [ 3, 2 ] ]

and as you can see {3,2} is a duplicate of {2,3}. How can I change my code so it returns only the unique sequences?

M Oehm

You can enforce that the next pick is greater than the current one by calling the recursive helper function with i instead of index:

for (let i = index; i <= n; i++){
    arr.push(i);
    helper(m - 1, n - i, arr, result, i + 1);
    arr.pop();
}

This will yield unique sequences where the elements are sorted in ascending order. If you call it with just i, you allow duplicate picks.

Changing this will allow zero as pick unless you call the helper with index 1 from the wrapper function:

let result = [];
helper(m, n, [], result, 1);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Find numbers with largest sum (Javascript)

분류에서Dev

Java regex to find a sequence of a single char and a sequence of numbers

분류에서Dev

Java regex to find a sequence of a single char and a sequence of numbers

분류에서Dev

I'm trying to find the sum of primes below 2 million in Java

분류에서Dev

How to find sum of n number using clojure for loop macro?

분류에서Dev

input and sum the numbers - java

분류에서Dev

Best algorithm to find N unique random numbers in VERY large array

분류에서Dev

Appending a sequence of numbers to an existing vector

분류에서Dev

mongodb $ sum only postive Numbers

분류에서Dev

Creating a Sum of numbers app with JavaScript

분류에서Dev

Find the local maxima in a sequence of values

분류에서Dev

How to find the missing number in a sequence

분류에서Dev

How to find sequence in text file

분류에서Dev

How to find the sum of factorials

분류에서Dev

Wolfram Mathematica의 Sum [] 및 Sequence []

분류에서Dev

Find missing numbers in array

분류에서Dev

How to find first n number of prime numbers in python?unable to follow the scope of a variable?

분류에서Dev

Trying to find the sum of individual columns

분류에서Dev

WorksheetFunction.Sum returning zero for numbers

분류에서Dev

Sum numbers return from a php function

분류에서Dev

SQL SUM amouts by distinct bill's numbers

분류에서Dev

Sum numbers in filenames in a directory using command line

분류에서Dev

Matlab: find the length of longest substring in sequence

분류에서Dev

print the adjacent sum sequence of integers in C without array

분류에서Dev

Transform OneHot-encoded data from array to sequence of numbers

분류에서Dev

Octave - Creating a sequence of odd numbers between 1 and 10 separated by zeros

분류에서Dev

How to produce the following sequence of numbers through a php for statement

분류에서Dev

Make sum K with N coins

분류에서Dev

Excel: Find a subset of numbers that add to a given total?

Related 관련 기사

  1. 1

    Find numbers with largest sum (Javascript)

  2. 2

    Java regex to find a sequence of a single char and a sequence of numbers

  3. 3

    Java regex to find a sequence of a single char and a sequence of numbers

  4. 4

    I'm trying to find the sum of primes below 2 million in Java

  5. 5

    How to find sum of n number using clojure for loop macro?

  6. 6

    input and sum the numbers - java

  7. 7

    Best algorithm to find N unique random numbers in VERY large array

  8. 8

    Appending a sequence of numbers to an existing vector

  9. 9

    mongodb $ sum only postive Numbers

  10. 10

    Creating a Sum of numbers app with JavaScript

  11. 11

    Find the local maxima in a sequence of values

  12. 12

    How to find the missing number in a sequence

  13. 13

    How to find sequence in text file

  14. 14

    How to find the sum of factorials

  15. 15

    Wolfram Mathematica의 Sum [] 및 Sequence []

  16. 16

    Find missing numbers in array

  17. 17

    How to find first n number of prime numbers in python?unable to follow the scope of a variable?

  18. 18

    Trying to find the sum of individual columns

  19. 19

    WorksheetFunction.Sum returning zero for numbers

  20. 20

    Sum numbers return from a php function

  21. 21

    SQL SUM amouts by distinct bill's numbers

  22. 22

    Sum numbers in filenames in a directory using command line

  23. 23

    Matlab: find the length of longest substring in sequence

  24. 24

    print the adjacent sum sequence of integers in C without array

  25. 25

    Transform OneHot-encoded data from array to sequence of numbers

  26. 26

    Octave - Creating a sequence of odd numbers between 1 and 10 separated by zeros

  27. 27

    How to produce the following sequence of numbers through a php for statement

  28. 28

    Make sum K with N coins

  29. 29

    Excel: Find a subset of numbers that add to a given total?

뜨겁다태그

보관