- Related Questions & Answers
- Program to find number of ways to arrange n rooks so that they cannot attack each other in Python
- Program to find the minimum cost to arrange the numbers in ascending or descending order in Python
- How can Tensorflow be used to export the model so that it can be used later?
- Program to merge intervals and sort them in ascending order in Python
- How to position Tkinter widgets so that they are not stuck together?
- Program to find shortest sublist so after sorting that entire list will be sorted in Python
- Python program to sort out words of the sentence in ascending order
- Program to sort a given linked list into ascending order in python
- Python program to sort the elements of an array in ascending order
- 8085 Program to perform bubble sort in ascending order
- 8085 Program to perform selection sort in ascending order
- Program to find overlapping intervals and return them in ascending order in Python
- How can we sort MySQL output in ascending order?
- Reveal Cards In Increasing Order in C++
- 8086 program to sort an integer array in ascending order

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a list of cards, and we want to order the cards in a way so that they are revealed in ascending order. As we know, the cards are revealed in this manner: 1. The top most card is removed and revealed and then next card is gone to the back. 2. Step 1 is repeated until there's no more cards. We have to find an ordering of the cards such that they are revealed in ascending order.

So, if the input is like cards = [1, 2, 3, 4, 5, 6, 7, 8], then the output will be [1, 5, 2, 7, 3, 6, 4, 8], as 1 is removed and 5 is moved to the back, current situation [2, 7, 3, 6, 4, 8, 5]. 2 is removed and 7 is moved to the back, current situation [3, 6, 4, 8, 5, 7] 3 is removed and 6 is moved to the back, current situation [4, 8, 5, 7, 6] 4 is removed and 8 is moved to the back, current situation [5, 7, 6, 8] 5 is removed and 7 is moved to the back, current situation [6, 8, 7]. 6 is removed and 8 is moved to the back, current situation [7, 8]. 7 is removed and there is only one card [8]. Then remove [8]

To solve this, we will follow these steps −

- sort the list cards
- idx:= a list with elements 0 to length of cards
- order:= a new list
- q:= a queue and insert elements of idx
- while q is non-zero, do
- delete element from left of q and insert into order
- if q is non-zero, then

- ans:= make a list of size cards, and fill with 0
- for each element i from order and card from cards, do
- ans[i]:= card

- return ans

Let us see the following implementation to get better understanding −

from collections import deque class Solution: def solve(self, cards): cards.sort() idx=[i for i in range(len(cards))] order=[] q=deque(idx) while q: order.append(q.popleft()) if q: q.append(q.popleft()) ans=[0 for _ in cards] for i,card in zip(order,cards): ans[i]=card return ans ob = Solution() print(ob.solve([1, 2, 3, 4, 5, 6, 7, 8]))

[1, 2, 3, 4, 5, 6, 7, 8]

[1, 5, 2, 7, 3, 6, 4, 8]

Advertisements