Disk tower

Your task is to construct a tower in days by following these conditions:

1.Every day you are provided with one disk of distinct size.

2.The disk with larger sizes should be placed at the bottom of the tower.

3.The disk with smaller sizes should be placed at the top of the tower.

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

1. 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 ith day.

Example:

Input: N = 5, arr = {4, 5, 1, 2, 3}

Output:

5 4 3 2 1

Approach:

C++

#include <bits/stdc++.h>
using namespace std;

vector<vector<int>> diskTower(vector<int&arr)
{

    int n = arr.size();
    vector<vector<int>> res;
    vector<intaux(n + 10);
    int expected = n;
    for (int i = 0i < ni++)
    {
        int a = arr[i];
        aux[a] = 1;
        vector<inttmp;
        while (expected >= 1 && aux[expected] == 1)
        {
            tmp.push_back(expected);
            expected--;
        }

        res.push_back(tmp);
    }

    return res;
}

int main()
{

    int N = 5;

    vector<intarr = {45123};

    vector<vector<int>> res = diskTower(arr);
    for (int i = 0i < res.size(); i++)
    {
        for (int j = 0j < res[i].size(); j++)
        {
            cout << res[i][j] << " ";
        }
        cout << "\n";
    }

    return 0;
}


Read Interview Questions

Exception Handling Interview Questions

DBMS Interview Questions Set -1

DBMS Interview Questions Set -2

SQL Interview Question Set -1

SQL Interview Question Set -2

JPA Interview Questions Set -1

JPA Interview Question Set -2

Hibernate Interview Questions

Spring Boot Interview Questions Set 1

Spring Boot Interview Questions Set 2


No comments:

Post a Comment