Find all combinations of size at least k to n


Vietnam's

I am struggling to figure out the formula to solve this problem:

Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k

E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3]

  • The array can contain repeated numbers.

For example: A=[1,1,2] k = 2 output = 3 // [1,1],[1,2],[1,1,2]but [1,2],[2,1], etc.not accepted.

I can use backtrackingTLE to solve it . I've been trying to find a formula from a question like all combinations nfound or all combinations of sizes found kwithout success.

So far I have figured out this table:

row = k
col = n
   1 2 3 4 5
  ---------
1| 1 2 3 4 5
2|   1 3 6 10
3|     1 4 10
4|       1 5 

The formula (not what I want) is:

combinations of size i with j numbers:
dp[i][j] = dp[i][j-1] + dp[i-1][j-1]
count(n,k) combinations of size k with n numbers
count(2,1) = 2
count(4,3) = count(3,3) + count(3,2) = 1 + 3 = 4
count(5,2) = count(4,2) + count(4,1) = 6 + 4 = 10
and so on

Even pseudocode would be greatly appreciated.

Update: Based on Oliver Dain's answer, if you're interested, use the following code

def count_combinations(n, k):
    count = 0
    for i in range(k, n + 1):
        count += math.factorial(n)/(math.factorial(i)*math.factorial(n - i))
    return int(count)
Oliver Dain

First find out how many unique values ​​are in the array (eg in most programming languages ​​you can put them into a setand then find the size of the set). Assume uunique value. Your answer, then, is that u choose the ksum of p for all values ​​of p between u(and both ends) of and .

Related


Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

Get all n-size combinations with k-size letters

Scion 4581 Can someone help me? I am trying to find the formula and write a piece of code in PHP language which makes the next step Imagine we have 3 types of things, k = 1, 2, 3, and the length of this number can be different (n length), but adjacent types sh

make all combinations of size k from 1 to number n

Ananya Given two numbers n and k, you have to find all possible combinations of k numbers in 1...n. I am using DFS algorithm to achieve this. But my ansarray returns None, whereas if I try to print temp, the combination is generated correctly. What am I doing

make all combinations of size k from 1 to number n

Ananya Given two numbers n and k, you have to find all possible combinations of k numbers in 1...n. I am using DFS algorithm to achieve this. But my ansarray returns None, whereas if I try to print temp, the combination is generated correctly. What am I doing

Find all unique combinations of n numbers between 1 and k

Onoga I want a list containing all possible sets of five (or n) numbers between 1 and 63 (or more generally 1 and k) If computation time is not an issue, I can do something like #Get all combenations of numbers between 1 and 63 indexCombinations <- expand.gr

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

Algorithm to generate n combinations of size k characters

alex_and_ra I need an algorithm that generates all combinations of size n of k characters. For example, if I have n=1 and k={a,b}, the result should be: a b If n=3 and k={a,b}, the result should be: a a a a a b a b a a b b b a a b a b b b a b b b Can someone

All possible combinations of N objects in K buckets

Aliresa Norri Say I have 3 boxes labeled A, B, C and I have 2 balls, B1 and B2. I want to get all possible combinations of these balls in a box. Note that it is important to know the balls in each box, which means that B1 and B2 are not the same. A B