Algorithm (especially for c++) to show Every Permutation

You want all permutations of each member of the powerset of the input. permSub(“abc”, “”) func permSub(input, perm) print perm if input = “” return for i = 0 to input.length-1 permSub(input[0..i]+input[i+1..input.length), perm+input[i] end end Where input[i..j] represents the sub string of input from i(inclusive) to j(exclusive), and + is string concatenation. Note that this … Read more

Recursive C function

Let’s construct our number that doesn’t have three consecutive ones from left to right. It necessarily starts with at most two 1s, so it starts with either no 1s, a single 1, or two 1s. In other words, our number starts with either 0, 10 or 110. In all of these cases, the only restriction … Read more

Why does this recursion return 0?

Your base case is return 0. After the line return n * bounce(n – 1) where n is 1, bounce(0) will be executed, returning 0 and multiplying all your previous results by 0. Following the calls, we see: 5>=1, so return 5*bounce(4) 4>=1, so return 5*4*bounce(3) 3>=1, so return 5*4*3*bounce(2) 2>=1, so return 5*4*3*2*bounce(1) 1>=1, … Read more