Program to make a histogram of an array
Last Updated :
05 Aug, 2024
Given an array of integers, print histogram of array values. Examples:
Input : 0 11 2 13 5 12 8 11 12 9
Output :
13 | x
12 | x x x
11 | x x x x x
10 | x x x x x
9 | x x x x x x
8 | x x x x x x x
7 | x x x x x x x
6 | x x x x x x x
5 | x x x x x x x x
4 | x x x x x x x x
3 | x x x x x x x x
2 | x x x x x x x x x
1 | x x x x x x x x x
0 | x x x x x x x x x x
---------------------------------------
0 11 2 13 5 12 8 11 12 9
Input : 10 9 12 4 5 2 8 5 3 1
Output :
12 | x
11 | x
10 | x x
9 | x x x
8 | x x x x
7 | x x x x
6 | x x x x
5 | x x x x x x
4 | x x x x x x x
3 | x x x x x x x x
2 | x x x x x x x x x
1 | x x x x x x x x x x
0 | x x x x x x x x x x
---------------------------------------
10 9 12 4 5 2 8 5 3 1
The idea is to print the given histogram row by row. For every element, we check if it is greater than or equal to current row. If yes, put a 'x' for that element. Else we put a space.
CPP
// CPP program to make histogram of an array
#include <bits/stdc++.h>
using namespace std;
void printHistogram(int arr[], int n)
{
int maxEle = *max_element(arr, arr + n);
for (int i = maxEle; i >= 0; i--) {
cout.width(2);
cout << right << i << " | ";
for (int j = 0; j < n; j++) {
// if array of element is greater
// then array it print x
if (arr[j] >= i)
cout << " x ";
// else print blank spaces
else
cout << " ";
}
cout << "\n";
}
// print last line denoted by ----
for (int i = 0; i < n + 3; i++)
cout << "---";
cout << "\n";
cout << " ";
for (int i = 0; i < n; i++) {
cout.width(2); // width for a number
cout << right << arr[i] << " ";
}
}
// Driver code
int main()
{
int arr[10] = { 10, 9, 12, 4, 5, 2,
8, 5, 3, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printHistogram(arr, n);
return 0;
}
Java
// Java program to make histogram of an array
import java.util.Arrays;
class Main {
static void printHistogram(int[] arr, int n) {
int maxEle = Arrays.stream(arr).max().getAsInt();
for (int i = maxEle; i >= 0; i--) {
System.out.format("%2d | ", i);
// if array of element is greater
// then array it print x
for (int j = 0; j < n; j++) {
if (arr[j] >= i) {
System.out.print(" x ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
for (int i = 0; i < n + 3; i++) {
System.out.print("---");
}
System.out.println();
System.out.print(" ");
for (int i = 0; i < n; i++) {
System.out.format("%2d ", arr[i]);
}
}
// Driver code
public static void main(String[] args) {
int[] arr = { 10, 9, 12, 4, 5, 2, 8, 5, 3, 1 };
int n = arr.length;
printHistogram(arr, n);
}
}
Python
def print_histogram(arr, n):
maxEle = max(arr)
for i in range(maxEle, -1, -1):
print(f"{i:2d} | ", end="")
# if array of element is greater
# then array it print x
for j in range(n):
if arr[j] >= i:
print(" x ", end="")
else:
print(" ", end="")
print()
for i in range(n + 3):
print("---", end="")
print()
print(" ", end="")
for i in range(n):
print(f"{arr[i]:2d} ", end="")
print()
# Driver code
arr = [10, 9, 12, 4, 5, 2, 8, 5, 3, 1]
n = len(arr)
print_histogram(arr, n)
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Gfg
{
static void PrintHistogram(int[] arr, int n)
{
int maxEle = arr.Max();
for (int i = maxEle; i >= 0; i--)
{
Console.Write(i.ToString().PadLeft(2) + " | ");
// if array of element is greater
// then array it print x
for (int j = 0; j < n; j++)
{
if (arr[j] >= i)
Console.Write(" x ");
else
Console.Write(" ");
}
Console.WriteLine();
}
for (int i = 0; i < n + 3; i++)
Console.Write("---");
Console.WriteLine();
Console.Write(" ");
for (int i = 0; i < n; i++)
{
Console.Write(arr[i].ToString().PadLeft(2) + " ");
}
}
static void Main(string[] args)
{
int[] arr = { 10, 9, 12, 4, 5, 2, 8, 5, 3, 1 };
int n = arr.Length;
PrintHistogram(arr, n);
}
}
JavaScript
// JS program to make histogram of an array
function printHistogram(arr) {
let n = arr.length;
let maxEle = Math.max(...arr);
for (let i = maxEle; i >= 0; i--) {
process.stdout.write(i.toString().padStart(2, ' ') + " | ");
for (let j = 0; j < n; j++) {
// if array of element is greater
// then array it print x
if (arr[j] >= i)
process.stdout.write(" x ");
// else print blank spaces
else
process.stdout.write(" ");
}
console.log();
}
// print last line denoted by ----
for (let i = 0; i < n + 3; i++)
process.stdout.write("---");
console.log();
process.stdout.write(" ");
for (let i = 0; i < n; i++) {
process.stdout.write(arr[i].toString().padStart(2, ' ') + " ");
}
}
// Driver code
let arr = [10, 9, 12, 4, 5, 2, 8, 5, 3, 1];
printHistogram(arr);
Output:
12 | x
11 | x
10 | x x
9 | x x x
8 | x x x x
7 | x x x x
6 | x x x x
5 | x x x x x x
4 | x x x x x x x
3 | x x x x x x x x
2 | x x x x x x x x x
1 | x x x x x x x x x x
0 | x x x x x x x x x x
---------------------------------------
10 9 12 4 5 2 8 5 3 1
Time complexity: O(N*maxEle) where N is size of given array and maxEle is maximum element in the array
Auxiliary space: O(1)
Similar Reads
How to Make a Simple Histogram with Altair in Python? Prerequisites: Altair Simple Histogram is the representation of frequency distribution by means of rectangles whose width represents the class interval. Histogram is the graphical representation that organizes grouped data points into the specified range. By using histogram we can visualize large am
4 min read
Jagged Array or Array of Arrays in C with Examples Prerequisite: Arrays in CJagged array is array of arrays such that member arrays can be of different sizes, i.e., we can create a 2-D array but with a variable number of columns in each row. These type of arrays are also known as Jagged arrays. Example:arr[][] = { {0, 1, 2}, {6, 4}, {1, 7, 6, 8, 9},
3 min read
How to Solve Histogram Equalization Numerical Problem in MATLAB? Histogram Equalization is a mathematical technique to widen the dynamic range of the histogram. Sometimes the histogram is spanned over a short range, by equalization the span of the histogram is widened. In digital image processing, the contrast of an image is enhanced using this very technique. U
3 min read
Find array elements that are greater than average Given an array of numbers, print all those elements that are greater than average. Examples: Input : 5, 4, 6, 9, 10 Output : 9 10 Explanation: avg = 5 + 4 + 6 + 9 + 10 / 5; avg = 34 / 5 avg = 6.8 Elements greater than 6.8 are 9 and 10 Input : 1, 2, 4, 0, 5 Output : 4 5 1) Find average of elements. 2
5 min read
Histogram Reconstruction In this article, we will discuss âDiagram to Histogramâ also known as âInterval Findingâ. While dealing with statistical data, diagrams are represented with single points(or the corresponding numbers) like the stars, which is supposed to be a histogram with a certain width as shown in the below figu
8 min read