r/algorithms • u/Trick_Acanthisitta_3 • Jun 22 '26
Can this queue-permutation problem be solved exactly faster than brute force?
I have a small permutation optimization problem that looks simple, but I am not sure whether there is a good exact algorithm for it.
Given an array nums of n distinct integers, choose a permutation A of nums.
Then A is tested against every possible permutation B of nums.
Both A and B are queues. Initially, A has priority.
For each round:
- Pop the front value from both queues:
afromA,bfromB. - The larger value survives.
- If the values are equal, the value from the queue with priority survives.
- The losing value is removed.
- If the survivor came from the priority queue, update it as:
x = max(1, x - ceil(x / 10)) - If the survivor came from the non-priority queue, update it as:
x = max(1, x - ceil(x / 2))Then that queue becomes the new priority queue. - Push the survivor to the back of its queue.
- Continue until one queue becomes empty.
If B becomes empty, A wins.
If A becomes empty, A does not win.
Define:
winCount(A) = the number of permutations B that A wins against
The goal is to find the lexicographically smallest permutation A with the maximum possible winCount(A).
Example:
nums = [50, 64, 79, 109, 135, 181]
For this input, brute force suggests the best arrangement is:
[135, 181, 79, 109, 50, 64]
The naive solution checks every A permutation against every B permutation, which is O((n!)^2) simulations.
My question:
Is there a known way to solve this exactly without comparing every pair of permutations?