- Related Questions & Answers
- Print array elements in alternatively increasing and decreasing order in C++
- Maximum sum of increasing order elements from n arrays in C++
- Maximum sum of increasing order elements from n arrays in C++ program
- Count no. of columns that are not sorted in increasing order in C++
- Increasing Subsequences in C++
- Sort Tuples in Increasing Order by any key in Python program
- How to sort matrix columns independently in increasing order in R?
- Program to arrange cards so that they can be revealed in ascending order in Python
- Python program to sort tuples in increasing order by any key.
- Monotone Increasing Digits in C++
- Maximum Points You Can Obtain from Cards in C++
- Rearranging cards into groups in JavaScript
- Count all increasing subsequences in C++
- Make Array Strictly Increasing in C++
- Longest Continuous Increasing Subsequence in C++

- 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 deck of cards; every card has a one unique number. We can order the deck in any order that we want. So Initially, all the cards start face down (unrevealed) in one deck. Now, we do the following steps multiple times, until all cards are revealed −

Suppose we have a deck of cards; every card has a one unique number. We can order the deck in any order that we want. So Initially, all the cards start face down (unrevealed) in one deck. Now, we do the following steps multiple times, until all cards are revealed −

If there are still cards in the deck, then put the next top card of the deck at the bottom of the deck.

If there are still unseen cards, go back to step 1. Otherwise, stop the process.

So we have to return an ordering of the deck that would reveal the cards in increasing order.

Now the first entry in the answer is considered to be the top of the deck.

So if the input is like [17,13,11,2,3,5,7], then the output will be [2,13,3,11,5,17,7], Let say we have reordered that into [2,13,3,11,5,17,7], now 2 is on top, after seeing 2, move 13 to the last, so the deck will be like [3,11,5,17,7,13], then remove 3, and do the step again. So the deck will be [5,17,7,13,11], after that remove 5, then after moving top to bottom, the array will be [7,13,11,17], then by doing same, the deck structures will be [11,17,13], [13.17], [17], then delete 17.

To solve this, we will follow these steps −

At first sort the deck, set n := size of the deck

define a queue q, and an array called ans of size n

insert consecutive i elements into q, where i is ranging from 0 to n – 1

for i in range 0 to n – 1

x := front element of q, then delete from queue

ans[x] := deck[i]

x := front element of q and delete from queue

insert x into q

return ans

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h> using namespace std; void print_vector(vector<int> v){ cout << "["; for(int i = 0; i<v.size(); i++){ cout << v[i] << ", "; } cout << "]"<<endl; } class Solution { public: vector<int> deckRevealedIncreasing(vector& deck) { sort(deck.begin(), deck.end()); int n = deck.size(); queue <int> q; vector <int> ans(n); for(int i = 0; i < n; i++)q.push(i); int x; for(int i = 0; i < n; i++){ x = q.front(); q.pop(); ans[x] = deck[i]; x = q.front(); q.pop(); q.push(x); } return ans; } }; main(){ vector<int> v1 = {17,13,11,2,3,5,7}; Solution ob; print_vector(ob.deckRevealedIncreasing(v1)); }

[17,13,11,2,3,5,7]

[2,13,3,11,5,17,7]

Advertisements