Permutation Game - 2nd input case - explanation - algorithm

Permutation Game (30 Points)
Alice and Bob play the following game:
1) They choose a permutation of the first N numbers to begin with.
2) They play alternately and Alice plays first.
3) In a turn, they can remove any one remaining number from the permutation.
4) The game ends when the remaining numbers form an increasing sequence. The person who played the last turn (after which the sequence becomes increasing) wins the game.
Assuming both play optimally, who wins the game?
Input:
The first line contains the number of test cases T. T test cases follow. Each case contains an integer N on the first line, followed by a permutation of the integers 1..N on the second line.
Output:
Output T lines, one for each test case, containing "Alice" if Alice wins the game and "Bob" otherwise.
Constraints:
1 <= T <= 100
2 <= N <= 15
The permutation will not be an increasing sequence initially.
Sample Input:
2
3
1 3 2
5
5 3 2 1 4
Sample Output:
Alice
Bob
Explanation: For the first example, Alice can remove the 3 or the 2 to make the sequence increasing and wins the game.
Can someone please help me out on the second input case: 5 3 2 1 4
The increasing sequences possible are:
1) 3 4 - Removing 5 , 2 , 1 in any sequence
2) 2 4 - Removing 5 , 3 , 1 in any sequence
3) 1 4 - Removing 5 , 3 , 2 in any sequence
So the output should be Alice?
Please do not share any code. Thanks

If Alice removes any of 5,3,2,1 then Bob removes 4. So, the increasing sequence can be of only one element, elements can be removed in any order. Hence, Bob wins.
If Alice removes 4, then also the increasing sequence has to be of one element. Bob wins.
So, Bob wins.

A possible case might be 4 or 5 is considered as increasing seq
As the input parameters are n>=2
But Alice would play optimally and remove 5 to win

NOTE: This isn't a programming problem and really doesn't belong on this site...
It sure looks like Alice should be the winner of the second test case.
Flow:
// Start state
5 3 2 1 4
// Alice remove 5
3 2 1 4
// Bob remove 3, 2, or 1
(2 1 4) or (3 1 4) or (3 2 4)
// Alice remove first number remaining
(1 4) or (2 4)
// Alice won!

Related

Sort a deck of cards [duplicate]

This question already has answers here:
Sorting a deque using limited operations?
(3 answers)
Closed 6 years ago.
Given a deck of N cards. You have to sort them using following permissible operations:
You can see top 2 cards.
You can swap them.
You can insert top at the bottom.
Any ideas?
This seems to be a simple case of bubblesort, a very inefficient sorting algorithm that is described by smaller elements bubbling to the top (assuming that you are sorting in ascending order). The modified algorithm I'm about to present is very similar to the original bubblesort algorithm, so I am going to quickly explain the original first. Bubblesort (for ascending order) works as follows,
The first element in a list is marked.
If the element to the right of the marked element is less than the marked element, then the two elements are swapped.
The marker moves to the right one spot regardless of the outcome of step two
Steps two and three are repeated until the marked element becomes the last element in the list. Just for clarification, this means that when step 3 causes the last element to be marked, the iteration is over and a new iteration starts.
The four steps above are repeated until an iteration occurs where the marker goes through each element in the list without a single swap occurring. Here's an example from wikipedia, https://en.wikipedia.org/wiki/Bubble_sort#Step-by-step_example
So let's modify bubblesort so that a couple things change to fit the deck of cards scenario. Let's not think of the deck of cards as a deck, but more as a list. Yes, the first card in the deck will constantly be changing with each iteration of our modified bubblesort but can we make it so that cards are shifting while still maintaining the position of the first card in the deck? This question is to key to solving the problem. What I am saying is that moving the cards to the bottom of the deck will not change in the initial order of the cards, only swapping will. For example, consider this deck of cards where leftmost is the top and rightmost is the bottom:
NOTE: (*) signifies marked card
*5 3 1 2 6
In the algorithm that will later be explained, moving 5 to the bottom of the deck will make the deck look like this
3 1 2 6 *5
Notice how 5 is now at the bottom of the deck, but the order is still preserved. The * symbol signifies the first card in the list/deck, so if read from left to right, starting from 5 and looping back to 3, the order is preserved.
Now to the algorithm, how do we use what I just said to make this modified version of bubblesort as similar to the original? Simple, use this marking mechanism to make it so that we're not really sorting a deck, but rather a list of numbers. Before starting the algorithm, mark the top card in the deck. Here are the rest of the steps for each iteration of bubblesort:
Compare the top card with the card below it. If the top card is greater, then swap with the card below. If the marked card was swapped, then unmark the previously marked card and mark the new card at the top of the deck.
Place the top card of the deck to the bottom.
Steps 1 and 2 are repeated until the marked card resurfaces to being the second card in the deck (card below the top card)
Put the top card to the bottom of the deck, making the marked card the top card in the deck.
These steps are repeated for each iteration of the algorithm until an iteration occurs where no swaps were made. Here is an example to showcase the modified bubblesort:
NOTE: (*) signifies marked card
Iteration One:
5 3 1 2 6 //Mark the first card in the deck before starting
*5 3 1 2 6 //Compare 5(top card) with 3(card below it)
3 *5 1 2 6 //5 > 3 so swap
*3 5 1 2 6 //Since the marked card (5) was swapped, 3 becomes the new marked
//card to preserve original order of the deck
5 1 2 6 *3 //Top card is placed at the bottom
1 5 2 6 *3 //5 > 1 so swap
5 2 6 *3 1 //Put 1 at the bottom
2 5 6 *3 1 //5 > 2 so swap
5 6 *3 1 2 //Put 2 at the bottom
5 6 *3 1 2 //5 < 6 so no swap
6 *3 1 2 5 //Put 5 at the bottom
*3 1 2 5 6 //Marked card is second to top card, so put 6 at the bottom
//Marked card is now at the top, so new iteration begins
Before going into iteration two, I would like to point out that if you ran the original bubblesort, the resulting sequence from one iteration would be the same as the resulting sequence from one iteration of our modified algorithm.
Iteration Two:
*3 1 2 5 6 //3 > 1, so swap
*1 3 2 5 6 //Remark accordingly since the former marked card was swapped
3 2 5 6 *1 //Place 1 at the bottom
2 3 5 6 *1 //3 > 2, so swap
3 5 6 *1 2 //Place 2 at the bottom
3 5 6 *1 2 //3 < 5 so no swap
5 6 *1 2 3 //Place 3 at the bottom
5 6 *1 2 3 //5 < 6 so no swap
6 *1 2 3 5 //Place 5 at the bottom.
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration
Iteration Three:
*1 2 3 5 6 //1 < 2 so no swap
2 3 5 6 *1 //Place 1 at the bottom
3 5 6 *1 2 //2 < 3 so no swap and place 2 at the bottom
5 6 *1 2 3 //3 < 5 so no swap and place 3 at the bottom
6 *1 2 3 5 //5 < 6 so no swap and place 5 at the bottom
*1 2 3 5 6 //Since marked card is second to top card, place 6 at the bottom and end iteration.
We now know to end the algorithm because an entire iteration has occurred without any swaps occurring, so the deck is now sorted. As for runtime, the worst case occurs, in terms of swaps, when the max number of iterations occurs which is n (size of the deck) times. And for each iteration, the worst case number of swaps occurs, which is also n times. So the big O is n*n or O(n^2).

smallest number greater than given number which is a palindrome

This was asked in an interview. Given a number, say 900, output the smallest palindrome greater than the number, 909 in this case. I gave a brute force solution that checks every number but I'm assuming there's a better way to go about this
Just for fun, here's a simple implementation in Python (using essentially the same algorithm as described by Guntram Blohm).
def next_palindrome(n):
"""
Given a non-negative integer n, return the first integer strictly
greater than n whose decimal representation is palindromic.
"""
s = str(n + 1)
l = len(s)
if s[:l//2][::-1] < s[(l+1)//2:]:
head = str(int(s[:(l+1)//2])+1)
else:
head = s[:(l+1)//2]
return int(head + head[:l//2][::-1])
And some sample output:
>>> next_palindrome(123)
131
>>> next_palindrome(4321)
4334
>>> next_palindrome(999)
1001
Copy the first digit to the last, second digit to the second-last etc until you reach the center digit (or center 2 digits if there is an even number of digits).
If the resulting number is smaller than the original number, increase the center digit/center 2 digits by one. If they are 9, set them to zero and retry with the 2 digits next to them, moving outwards until you hit a non-9.
Edit:
If the loop that moves outwards never hits a non-9, prepend a 1 to the string, set all digits except the last one to 0, and the last one to 1. This is the same as adding 2 to the number.
Though what has been answered above is absolutely correct. Just to add more understanding :)
There can be three different types of inputs that need to be handled separately.
1) The input number is palindrome and has all 9s. For example “9 9
9″. Output should be “1 0 0 1″
2) The input number is not palindrome. For example “1 2 3 4″. Output
should be “1 3 3 1″
3) The input number is palindrome and doesn’t have all 9s. For example
“1 2 2 1″. Output should be “1 3 3 1″.
Solution for input type 1
is easy. The output contains n + 1 digits where the corner digits are 1, and all digits between corner digits are 0.
Now let us first talk about input type 2 and 3. Let us first define the following two terms:
Left Side: The left half of given number. Left side of “1 2 3 4 5 6″ is “1 2 3″ and left side of “1 2 3 4 5″ is “1 2″
Right Side: The right half of given number. Right side of “1 2 3 4 5 6″ is “4 5 6″ and right side of “1 2 3 4 5″ is “4 5″
To convert to palindrome, we can either take the mirror of its left side or take mirror of its right side. However, if we take the mirror of the right side, then the palindrome so formed is not guaranteed to be next larger palindrome.
So, we must take the mirror of left side and copy it to right side. But there are some cases that must be handled in different ways. See the following steps.
We will start with two indices i and j. i pointing to the two middle elements (or pointing to two elements around the middle element in case of n being odd). We one by one move i and j away from each other.
Step 1. Initially, ignore the part of left side which is same as the corresponding part of right side. For example, if the number is “8 3 4 2 2 4 6 9″, we ignore the middle four elements. i now points to element 3 and j now points to element 6.
Step 2. After step 1, following cases arise:
Case 1: Indices i & j cross the boundary.
This case occurs when the input number is palindrome. In this case, we just add 1 to the middle digit (or digits in case n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.
For example, if the given number is “1 2 9 2 1″, we increment 9 to 10 and propagate the carry. So the number becomes “1 3 0 3 1″
Case 2: There are digits left between left side and right side which are not same. So, we just mirror the left side to the right side & try to minimize the number formed to guarantee the next smallest palindrome.
In this case, there can be two sub-cases.
2.1) Copying the left side to the right side is sufficient, we don’t need to increment any digits and the result is just mirror of left side. Following are some examples of this sub-case.
Next palindrome for “7 8 3 3 2 2″ is “7 8 3 3 8 7″
Next palindrome for “1 2 5 3 2 2″ is “1 2 5 5 2 1″
Next palindrome for “1 4 5 8 7 6 7 8 3 2 2″ is “1 4 5 8 7 6 7 8 5 4 1″
How do we check for this sub-case? All we need to check is the digit just after the ignored part in step 1. This digit is highlighted in above examples. If this digit is greater than the corresponding digit in right side digit, then copying the left side to the right side is sufficient and we don’t need to do anything else.
2.2) Copying the left side to the right side is NOT sufficient. This happens when the above defined digit of left side is smaller. Following are some examples of this case.
Next palindrome for “7 1 3 3 2 2″ is “7 1 4 4 1 7″
Next palindrome for “1 2 3 4 6 2 8″ is “1 2 3 5 3 2 1″
Next palindrome for “9 4 1 8 7 9 7 8 3 2 2″ is “9 4 1 8 8 0 8 8 1 4 9″
We handle this subcase like Case 1. We just add 1 to the middle digit (or digits in ase n is even) propagate the carry towards MSB digit of left side and simultaneously copy mirror of the left side to the right side.
SOURCE: http://www.geeksforgeeks.org/given-a-number-find-next-smallest-palindrome-larger-than-this-number/

Ascending Cardinal Numbers in APL

In the FinnAPL Idiom Library, the 19th item is described as “Ascending cardinal numbers (ranking, all different) ,” and the code is as follows:
⍋⍋X
I also found a book review of the same library by R. Peschi, in which he said, “'Ascending cardinal numbers (ranking, all different)' How many of us understand why grading the result of Grade Up has that effect?” That's my question too. I searched extensively on the internet and came up with zilch.
Ascending Cardinal Numbers
For the sake of shorthand, I'll call that little code snippet “rank.” It becomes evident what is happening with rank when you start applying it to binary numbers. For example:
X←0 0 1 0 1
⍋⍋X ⍝ output is 1 2 4 3 5
The output indicates the position of the values after sorting. You can see from the output that the two 1s will end up in the last two slots, 4 and 5, and the 0s will end up at positions 1, 2 and 3. Thus, it is assigning rank to each value of the vector. Compare that to grade up:
X←7 8 9 6
⍋X ⍝ output is 4 1 2 3
⍋⍋X ⍝ output is 2 3 4 1
You can think of grade up as this position gets that number and, you can think of rank as this number gets that position:
7 8 9 6 ⍝ values of X
4 1 2 3 ⍝ position 1 gets the number at 4 (6)
⍝ position 2 gets the number at 1 (7) etc.
2 3 4 1 ⍝ 1st number (7) gets the position 2
⍝ 2nd number (8) gets the position 3 etc.
It's interesting to note that grade up and rank are like two sides of the same coin in that you can alternate between the two. In other words, we have the following identities:
⍋X = ⍋⍋⍋X = ⍋⍋⍋⍋⍋X = ...
⍋⍋X = ⍋⍋⍋⍋X = ⍋⍋⍋⍋⍋⍋X = ...
Why?
So far that doesn't really answer Mr Peschi's question as to why it has this effect. If you think in terms of key-value pairs, the answer lies in the fact that the original keys are a set of ascending cardinal numbers: 1 2 3 4. After applying grade up, a new vector is created, whose values are the original keys rearranged as they would be after a sort: 4 1 2 3. Applying grade up a second time is about restoring the original keys to a sequence of ascending cardinal numbers again. However, the values of this third vector aren't the ascending cardinal numbers themselves. Rather they correspond to the keys of the second vector.
It's kind of hard to understand since it's a reference to a reference, but the values of the third vector are referencing the orginal set of numbers as they occurred in their original positions:
7 8 9 6
2 3 4 1
In the example, 2 is referencing 7 from 7's original position. Since the value 2 also corresponds to the key of the second vector, which in turn is the second position, the final message is that after the sort, 7 will be in position 2. 8 will be in position 3, 9 in 4 and 6 in the 1st position.
Ranking and Shareable
In the FinnAPL Idiom Library, the 2nd item is described as “Ascending cardinal numbers (ranking, shareable) ,” and the code is as follows:
⌊.5×(⍋⍋X)+⌽⍋⍋⌽X
The output of this code is the same as its brother, ascending cardinal numbers (ranking, all different) as long as all the values of the input vector are different. However, the shareable version doesn't assign new values for those that are equal:
X←0 0 1 0 1
⌊.5×(⍋⍋X)+⌽⍋⍋⌽X ⍝ output is 2 2 4 2 4
The values of the output should generally be interpreted as relative, i.e. The 2s have a relatively lower rank than the 4s, so they will appear first in the array.

Minimize maximum absolute difference in pairs of numbers

The problem statement:
Give n variables and k pairs. The variables can be distinct by assigning a value from 1 to n to each variable. Each pair p contain 2 variables and let the absolute difference between 2 variables in p is abs(p). Define the upper bound of difference is U=max(Abs(p)|every p).
Find an assignment that minimize U.
Limit:
n<=100
k<=1000
Each variable appear at least 2 times in list of pairs.
A problem instance:
Input
n=9, k=12
1 2 (meaning pair x1 x2)
1 3
1 4
1 5
2 3
2 6
3 5
3 7
3 8
3 9
6 9
8 9
Output:
1 2 5 4 3 6 7 8 9
(meaning x1=1,x2=2,x3=5,...)
Explaination: An assignment of x1=1,x2=2,x3=3,... will result in U=6 (3 9 has greastest abs value). The output assignment will get U=4, the minimum value (changed pair: 3 7 => 5 7, 3 8 => 5 8, etc. and 3 5 isn't changed. In this case, abs(p)<=4 for every pair).
There is an important point: To achieve the best assignments, the variables in the pairs that have greatest abs must be change.
Base on this, I have thought of a greedy algorithm:
1)Assign every x to default assignment (x(i)=i)
2)Locate pairs that have largest abs and x(i)'s contained in them.
3)For every i,j: Calculate U. Swap value of x(i),x(j). Calculate U'. If U'<U, stop and repeat step 3. If U'>=U for every i,j, end and output the assignment.
However, this method has a major pitfall, if we need an assignment like this:
x(a)<<x(b), x(b)<<x(c), x(c)<<x(a)
, we have to swap in 2 steps, like: x(a)<=>x(b), then x(b)<=>x(c), then there is a possibility that x(b)<<x(a) in first step has its abs become larger than U and the swap failed.
Is there any efficient algorithm to solve this problem?
This looks like http://en.wikipedia.org/wiki/Graph_bandwidth (NP complete, even for special cases). It looks like people run http://en.wikipedia.org/wiki/Cuthill-McKee_algorithm when they need to do this to try and turn a sparse matrix into a banded diagonal matrix.

game theory algorithms: how to proceed

Take an example: Permutation Game(interviewstreet.com). I would like to know how do I proceed in such questions.
P.S.: Please don't post the full algorithm(as that would spoil the fun), just a few pointers.
I would setup a small game with a small N and a random permutation and then draw a complete Alpha-Beta Tree...
http://en.wikipedia.org/wiki/Alpha-beta_pruning
of all possible moves, and then work bottom-up making the optimal choice for each player at each point.
Then generalize from there once you see the pattern.
In game theory terminology you need to use Backward Induction to find the Subgame Perfect Equilibrium.
N is rather small. In each turn, there are two possibilities for each number: remove that number or don't remove that number. Try both possibilities, resulting in an O(N*2^N) algorithm for each test case. In practice this will be lower since the game usually ends before all numbers are removed and you can cut the search short quite often.
So you want a backtracking function that tries all possibilities of removing the numbers and returns 1 if Alice wins and 2 if Bob wins. At depth k (first depth is k = 0), if k % 2 = 0, then it's Alice's turn. She wins if all the immediate recursive calls (that have depth k + 1) return 1. If at least one of them returns 2, then she loses, because there is at least one way for Bob to win.
Example run on 1 3 2:
k = 0 - Alice removes 1:
k = 1 - Bob removes 3 => Bob wins because only 2 remains
- Bob removes 2 => Bob wins because only 3 remains (note: this step
is not needed, as Bob already proved he can win at this k = 1)
- Alice removes 3 => Alice wins because 1 2 is increasing
- Alice removes 2 => Alice wins because 1 3 is increasing
So Alice definitely has a winning strategy (when both play optimally) if she removes 3 or 2 in the first move, because the recursive branches of these two never give Bob as the winner.
Example run on 5 3 2 1 4 (partial run):
k = 0 - Alice removes 5
k = 1 - Bob removes 3
k = 2 - Alice removes 2 => 1 4 => Alice wins
k = 1 - Bob removes 2
k = 2 - Alice removes 3 => 1 4 => Alice wins
k = 1 - Bob removes 1
k = 2 - Alice removes 3 => 2 4 => Alice wins
k = 1 - Bob removes 4
k = 2 - Alice removes 3
k = 3 - Whatever Bob removes, he wins
k = 2 - Alice removes 2
k = 3 - Whatever Bob removes, he wins
k = 2 - Alice removes 1
k = 3 - Whatever Bob removes, he wins
...
As you can see, there is at least one way for Bob to end up winning if Alice starts by removing 5. If you do the same for her other possibilities, you will probably get the same result. Thus, it's Bob who will definitely win if he plays optimally.
Pen and Paper
Use pen and paper, make sure you understand the rules of the game exactly, then think of all possible strategies for the game.
For a relatively simple problem like this, think backwards from the point when game is won, unrolling one step at a time and making sure the player who made that step could not have made a better step, that's the optimality requirement.
For more complicated problems, read up Wikipedia on game theory.

Resources