Your task is to construct a tower in N 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 lines denoting the disk sizes that can be put on the tower on the 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<int> aux(n + 1, 0);int expected = n;for (int i = 0; i < n; i++){int a = arr[i];aux[a] = 1;vector<int> tmp;while (expected >= 1 && aux[expected] == 1){tmp.push_back(expected);expected--;}res.push_back(tmp);}return res;}int main(){int N = 5;vector<int> arr = {4, 5, 1, 2, 3};vector<vector<int>> res = diskTower(arr);for (int i = 0; i < res.size(); i++){for (int j = 0; j < 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
JPA Interview Questions Set -1
Spring Boot Interview Questions Set 1
Spring Boot Interview Questions Set 2
No comments:
Post a Comment