DEV Community

Ravi Agheda
Ravi Agheda

Posted on

Daily Coding Problem #634

You are given a histogram consisting of rectangles of different heights. These heights are represented in an input list, such that [1, 3, 2, 5] corresponds to the following diagram

Screenshot from 2021-01-31 22-02-54


#include<iostream>
using namespace std;

int main(){
    int n;
    cout << "Enter size of the array -> ";
    cin >> n;
    int a[n],max = 0;

// input
    for(int i=0;i<n;i++){
        cin >> a[i];
        if(a[i] > max){
            max = a[i];
        }
    }
// output
    for(int i=max;i>0;i--){
        for(int j=0;j<n;j++){
            if(a[j] >= i)
                cout << "x";
            else
                cout << " ";
        }
        cout << endl;
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:
Screenshot from 2021-01-31 22-05-50

Top comments (0)