Recursive function that returns the number of possible combinations

Mahdi

I had an interview and was asked a question that I'd like to understand the solution.

The Question

Create a recursive function that returns the number of possible combinations of arrays of a given length that could be made from an array of non-repeating consecutive integers.

f(array, length) = Combinations

Example 1

  • array = [0,1,2,3]
  • length = 2
  • Combinations = 10 (all combinations: [0,0] [0,1] [0,2] [0,3] [1,1] [1,2] [1,3] [2,2] [2,3] [3,3])
  • Note that [0,0] is allowed but [1,0] is not because [0,1] is defined

Example 2

  • array = [0,1]
  • length = 3
  • Combinations = 4 (all combinations: [0,0,0] [0,0,1] [0,1,1] [1,1,1])

One "hint" was offered. The interviewer said the array itself shouldn't matter; the length was all that was needed.

meowgoesthedog

This algorithm can be expressed recursively because the solution can be expressed in terms of solutions for smaller inputs. "Smaller" here has two meanings:

  • A subset of the array; specifically the sub-array after the current element index

  • Solutions for smaller length; these can be added together to give the solution for length + 1

Stopping conditions:

  • When the array size A = 1 - only one combination can be generated

  • When the length L = 1 - number of combinations = number of elements in array

The fully recursive procedure is a surprisingly simple one-liner:

return [recursive call to rest of array, same length] + 
       [recursive call to same array, length - 1]

This is called dynamic programming.

Code:

int F(int A, int L)
{
    if (A <= 1) return 1;
    if (L <= 1) return A;
    return F(A - 1, L) + F(A, L - 1);
}

Tests:

  • F(4, 2) = 10
  • F(2, 3) = 4
  • F(3, 5) = 21 (trace it with pen-and-paper to see for yourself)

EDIT: I've given an elegant and simple solution, but I perhaps haven't explained it as well as @RoryDaulton. Consider giving his answer credit too.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Recursive function to get combinations of dynamic number of List

Recursive function to create number combinations in golang

Recursive function for combinations

Finding number of combinations possible

My find-biggest-number Recursive function returns a discremented value

Recursive function that returns the number of numbers greater then the first index of an array

Recursive factorial function program returns 0 for large number input in C

List all possible combinations of Ip addresses from given String using Recursive Function

How to eval a number into possible combinations

Find the number of possible combinations in a graph

Pseudocode or C# algorithm that returns all possible combinations sets for a number of variables

Returns in a recursive function

Recursive function that returns the remainder

Function that returns array of array combinations

Is it possible to tell if a function is recursive?

Is it possible to optimize this recursive function?

Find all possible function combinations

Python question -- lambda version of a recursive combinations function

Tail recursive Clojure function for computing combinations

What does "return a" in this recursive combinations function does?

R: Choosing specific number of combinations from all possible combinations

Python - recursive function returns None

Recursive prompt function returns null

TypeScript Recursive function returns undefined

Recursive function in Javascript returns undefined

Recursive function that returns the smallest divisor

Recursive Function Always Returns False

why this recursive function returns a tuple?

find the number of all possible combinations with conflicts