- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

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

# Reveal Cards In Increasing Order in C++

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 −

## Example

#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)); }

### Input

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

## Output

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