Algorithm to determine all possible ways a group of values can be removed from a sequence

(Because it was unclear in the original version of the question whether you meant to remove a subsequence or an unordered list, I’ve updated my answer to address both cases.) 1. Removing a sub-sequence in order We get a sequence of values, e.g. [1,5,1,3,4,2,3,2,1,3,1,2], and a sub-sequence of values to be removed from the first … Read more

Secret Santa – Generating ‘valid’ permutations

What you’re looking for is called a derangement (another lovely Latinate word to know, like exsanguination and defenestration). The fraction of all permutations which are derangements approaches 1/e = approx 36.8% — so if you are generating random permutations, just keep generating them, and there’s a very high probability that you’ll find one within 5 … Read more

Algorithm to get all the combinations of size n from an array (Java)? [closed]

This is a well-studied problem of generating all k-subsets, or k-combinations, which can be easily done without recursion. The idea is to have array of size k keeping sequence of indices of elements from the input array (which are numbers from 0 to n – 1) in increasing order. (Subset then can be created by … Read more