All Tracks Data Structures Queues Basics of Queues Problem

Disk tower

Data Structures, Hash Maps, Priority queue, Queue


Your task is to construct a tower in \(N\) days by following these conditions:

  • Every day you are provided with one disk of distinct size.
  • The disk with larger sizes should be placed at the bottom of the tower.
  • The disk with smaller sizes should be placed at the top of the tower.

The order in which tower must be constructed is as follows:

  • You cannot put a new disk on the top of the tower until all the larger disks that are given to you get placed.

Print \(N\) lines denoting the disk sizes that can be put on the tower on the \(i^{th}\) day.

Input format

  • First line: \(N\) denoting the total number of disks that are given to you in the \(N\) subsequent days
  • Second line: \(N\) integers in which the \(i^{th}\) integers denote the size of the disks that are given to you on the \(i^{th}\) day

Note: All the disk sizes are distinct integers in the range of  \({1}\ to\ {N}\).

Output format

Print \(N\) lines. In the \(i^{th}\) line, print the size of disks that can be placed on the top of the tower in descending order of the disk sizes.

If on the \(i^{th}\) day no disks can be placed, then leave that line empty.


\(1 \le N \le10^{6}\)

\(1 \le size\ of\ a\ disk \le N\)

4 5 1 2 3
5 4
3 2 1

On the first day, the disk of size 4 is given. But you cannot put the disk on the bottom of the tower as a disk of size 5 is still remaining.

On the second day, the disk of size 5 will be given so now disk of sizes 5 and 4 can be placed on the tower. 

On the third and fourth day, disks cannot be placed on the tower as the disk of 3 needs to be given yet. Therefore, these lines are empty. 

On the fifth day, all the disks of sizes 3, 2, and 1 can be placed on the top of the tower.

Time Limit: 1.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: Bash, C, C++, C++14, C++17, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, Java 14, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, Python 3.8, R(RScript), Racket, Ruby, Rust, Scala, Swift-4.1, Swift, TypeScript, Visual Basic


Initializing Code Editor...
Your Rating:


This Problem was Asked in

Nokia LnD

Challenge Name

Nokia Code Rally 2019

View All Notifications