Permutation sequence of N repeating numbers
Anonymous User
772

Given an array of 2N positive numbers where every number from 1 to N appears exactly twice in the array, find if there is a permutation possible such that for any given number X, there should be exactly X numbers between the two X's in the permutation. Also the valid permutation should use all the given 2N numbers.

Input: {1,1,2,2,3,3}
Output: true
Explanation: The permutation (3, 1, 2, 1, 3, 2) satisfies the given conditions. Between two 3's there are exactly 3 numbers, between two 2's there are exactly 2 numbers and between two 1's there is exactly 1 number and we are using all the given input numbers.

Input: {1,1,2,2,3,3,4,4}
Output: true
possible permutation: (4,1,3,1,2,4,3,2)
Explanation: In the above example the permutation is possible because between two 4's there are exactly 4 numbers, between two 3's there are exactly 3 numbers, between two 2's there are exactly 2 numbers and between two 1's there is exactly 1 number and we are using all the given input numbers.

Input: {1,1,2,2}
Output: false
the permutations {(2,1,1,2), (1,2,1,2) ....} doesn't satisfy the required condition as there is only 1 number between two 2's and there is no number between two 1's.

PS: Having 15+ yrs of experience and getting stuck in the same company for a long time, I have decided to start giving interviews. So I have attended an onsite interview at Adobe in the month of Jan this year, i haven't started leetcode by then and i just want to check my current standard. This is the only question i couldn't even get an approach. So I have posted this question through leetcode contribution page around 6months back thinking that it will be published with some approaches for the solution. When ever i ping them, i get an answer saying it is still in pipeline as there are too many questions pending for review, it is being evaluated whether it is really asked in an interview(I don't know how they will verify).
But one thing for sure, i am going to retire before leetcode publishes this question and i get an answer.

Comments (3)