Programming Articles

Page 1466 of 2547

Program to make a histogram of an array in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 3K+ Views

In this tutorial, we will be discussing a program to make a histogram by the data given inside an array.For this, we will be provided with integer values inside an array. Our task is to plot a histogram keeping the value of both coordinates x and y equal to the value provided in the array.Example#include using namespace std; void make_histogram(int arr[], int n){    int maxEle = *max_element(arr, arr + n);    for (int i = maxEle; i >= 0; i--) {       cout.width(2);       cout

Read More

Find longest sequence of 1's in binary representation with one flip in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 2K+ Views

Suppose we have one integer n. Inside that, we can make the one-bit flip to generate the longest sequence of 1s. Suppose the number is 13, so binary representation is 1101. If we make a one-bit flip as make 0 to 1, it will be 1111. This is the longest sequence of 1sTo solve this problem, we will walk through the bits of a given number. We will keep track of the current 1’s sequence length, and the previous 1’s sequence length. When a zero has found, then update the previous length. So if the next bit is 1, then ...

Read More

Find maximum level product in Binary Tree in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 209 Views

Suppose, one binary tree is given. It has positive and negative nodes. We have to find the maximum product at each level of it.Consider this is the tree, so the product of level 0 is 4, product of level 1 is 2 * -5 = -10, and product of level 2 is -1 * 3 * -2 * 6 = 36. So this is the maximum one.To solve this, we will perform the level order traversal of the tree, during traversal, process doing the nodes of different levels separately. Then get the maximum product.Example#include #include using namespace std; class Node ...

Read More

Find maximum product of digits among numbers less than or equal to N in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 513 Views

Suppose, we have one integer N > 0. The task is to find the maximum product of digits among numbers less than or equal to N. If the N is 390, then the result is 216, as the number 389 is making maximum product 3 * 8 * 9 = 216.To solve this problem, we will use the recursive approach. So if N = 0, then return 1, if the number N < 10, then return N, otherwise return max(max_product(N/10) * (N mod 10), max_product((N/10) - 1)*9)Example#include using namespace std; int max_product(int N) {    if (N == 0)   ...

Read More

Program to print 'N' alphabet using the number pattern from 1 to n in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 269 Views

In this tutorial, we will be discussing a program to print ‘N’ alphabet using the number pattern from 1 to n.For this we will have to print the english alphabet N. Our task is to determine the size of the letter and print it back using the numbers from 1 to n.Example#include using namespace std; //printing the letter N void print_N(int N){    int index, side_index, size;    int Right = 1, Left = 1, Diagonal = 2;    for (index = 0; index < N; index++) {       cout

Read More

Find minimum positive integer x such that a(x^2) + b(x) + c >= k in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 351 Views

Suppose we have four integers a, b, c and k. We have to find the minimum positive value x, such that the following equation satisfies −𝑎𝑥2+𝑏𝑥+𝑐 ≥𝑘If a = 3, b = 4, c = 5 and k = 6, then output will be 1To solve this, we will use the bisection approach. The lower limit will be 0 since x has to be a minimum positive integer.Example#include using namespace std; int getMinX(int a, int b, int c, int k) {    int x = INT8_MAX;    if (k

Read More

Program to print 2D shapes in C++

Ayush Gupta
Ayush Gupta
Updated on 11-Mar-2026 511 Views

In this tutorial, we will be discussing a program to print out 2D shapes.For this we will be provided with the various parameters required to make a shape such as radius, side length and side breadth, etc. And our task is to print a shape accordingly with no thickness.Example#include using namespace std; void print_circle(int radius){    for (int i = 0; i

Read More

Find closest number in array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 3K+ Views

Suppose we have an array A with n elements. And elements are sorted. We have to find the closest value to the given integer. The array may contain duplicate values and negative numbers. So if the array is like [2, 5, 6, 7, 8, 8, 9] and the target number is 4, then closest element is 5.We can solve this by traversing through the given array and keep track of absolute difference of current element with every element. Finally return the element that has minimum absolute difference.Example#include #include using namespace std; int getNearest(int x, int y, int target) {   ...

Read More

Find coordinates of the triangle given midpoint of each side in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 264 Views

Suppose we have three coordinates which are midpoint of sides of the triangle. We have to find the coordinates of the triangle. So if the inputs are like (5, 3), (4, 4), (5, 5), then output will be (4, 2), (4, 6), (6, 4).To solve this, we have to solve for X-coordinates and Y-coordinates separately. For X coordinate of vertices, let them be x1, x2, x3. Then, X-coordinate of middle points will be (x1 + x2)/2, (x2 + x3)/2, (x3 + x1)/2. If we observe the sum of these three expressions is equal to sum of X-coordinates. Now, we have ...

Read More

Find height of a special binary tree whose leaf nodes are connected in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 302 Views

Suppose we have a special binary tree, whose leaf nodes are connected to form a circular doubly linked list. We have to find its height. So the left pointer of the left most leaf will act as previous pointer of circular doubly linked list, and its right pointer will act as next pointer of the linked list.In this case the height finding strategy is similar to the normal binary search tree. We recursively calculate the height of the left and right subtrees of a node and assign height to the node is max of the two children + 1. But ...

Read More
Showing 14651–14660 of 25,466 articles
Advertisements