Questions tagged [permutations]

Permutations are arrangements of the numbers $1,\ldots,n$ in an arbitrary order.

Permutations are arrangements of the numbers $1,\ldots,n$ in an arbitrary order. There are $n!$ many permutations, forming a group known as the symmetric group.

232 questions
66
votes
3 answers

In-place algorithm for interleaving an array

You are given an array of $2n$ elements $$a_1, a_2, \dots, a_n, b_1, b_2, \dots b_n$$ The task is to interleave the array, using an in-place algorithm such that the resulting array looks like $$b_1, a_1, b_2, a_2, \dots , b_n, a_n$$ If the in-place…
Aryabhata
  • 6,291
  • 2
  • 36
  • 47
19
votes
2 answers

What's harder: Shuffling a sorted deck or sorting a shuffled one?

You have an array of $n$ distinct elements. You have access to a comparator (a black box function taking two elements $a$ and $b$ and returning true iff $a < b$) and a truly random source of bits (a black box function taking no arguments and…
14
votes
1 answer

Interesting problem on sorting

Given a tube with numbered balls (random). The tube has holes to remove a ball. Consider the following steps for one operation: You can pick one or more balls from the holes and remember the order in which you picked the balls. You need to tilt the…
rakesh
  • 141
  • 2
13
votes
2 answers

Counting permutations whose elements are not exactly their index ± M

I was recently asked this problem in an algorithmic interview and failed to solve it. Given two values N and M, you have to count the number of permutations of length N (using numbers from 1 to N) such that the absolute difference between any…
Gena
  • 151
  • 4
13
votes
2 answers

Efficient algorithm to generate two diffuse, deranged permutations of a multiset at random

Background $\newcommand\ms[1]{\mathsf #1}\def\msD{\ms D}\def\msS{\ms S}\def\mfS{\mathfrak…
11
votes
1 answer

Indexing into a pattern database - Korf's Optimal Rubik's Cube solution

As a fun project, I've been working on a C# implementation of Richard Korf's - Finding Optimal Solutions to Rubik's Cube Using Pattern Databases. https://www.cs.princeton.edu/courses/archive/fall06/cos402/papers/korfrubik.pdf I actually have it…
Cosmosis
  • 113
  • 4
11
votes
2 answers

Subset sum problem for permutations

Given permutations $g_1,\,\ldots, g_m \in S_n$ of size $n$ and target permutation $g \in S_n$, decide if there exists a subset of $\{g_1,\, \ldots, g_m\}$, which composition in some order (or, alternatively, as a variant of this problem, in the same…
9
votes
3 answers

Finding number of smaller elements for each element in an array efficiently

I am stuck on this problem: Given an array $A$ of the first $n$ natural numbers randomly permuted, an array $B$ is constructed, such that $B(k)$ is the number of elements from $A(1)$ to $A(k-1)$ which are smaller than $A(k)$. i) Given $A$…
damned
  • 231
  • 2
  • 5
9
votes
2 answers

Alternative to Hamming distance for permutations

I have two strings, where one is a permutation of the other. I was wondering if there is an alternative to Hamming distance where instead of finding the minimum number of substitutions required, it would find the minimum number of translocations…
9
votes
2 answers

Is there a "sorting" algorithm which returns a random permutation when using a coin-flip comparator?

Inspired by this question in which the asker wants to know if the running time changes when the comparator used in a standard search algorithm is replaced by a fair coin-flip, and also Microsoft's prominent failure to write a uniform permutation…
Joe
  • 4,105
  • 1
  • 21
  • 38
9
votes
2 answers

Find an optimal ordering

I came across this problem and am struggling to find a way to approach it. Any thoughts would be greatly appreciated! Suppose we are given a matrix $\{-1, 0, 1\}^{n\ \times\ k} $, for example, $$\begin{bmatrix} 1 & 0 & 1 & 0 & -1 \\ -1 &…
8
votes
1 answer

Searching the space of permutations

I'm given n objects, and a set of n permutations of these n objects (out of n! total permutations). There is a true underlying permutation, which I know is one among the set of n permutations, but I don't know which one. An oracle however knows the…
8
votes
4 answers

Stack Permutation Algorithm

I was recently designing a Forth stack machine. I have an atomic instruction which rotates the top N elements. For example if the top of the stack is on the left, then say the N=3 rotate instruction would do the following: A B C D -> C A B D A few…
7
votes
0 answers

What is the intuition behind Heap's Algorithm?

I am trying to get an intuition for Heap's Algorithm which is used to generate permutations of a given set. What I can't understand is why if n is even the letter swapped is i and when n is odd the letter shifted is at position 0. I know it works…
Kramer786
  • 223
  • 1
  • 3
7
votes
3 answers

What's a uniform shuffle?

What does it mean exactly a "uniform shuffle" algorithm ? Is this method considered a uniform shuffle ? public void shuffle(Comparable[] a) { int n = a.length; int k, j; for (int i = 0; i < n; i++) { k = StdRandom.uniform(n); //…
morfioce
  • 255
  • 3
  • 5
1
2 3
15 16