Found 7347 Articles for C++

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

Arnab Chakraborty
Updated on 19-Dec-2019 10:14:25

261 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 Live Demo#include using namespace std; int getMinX(int a, int b, int c, int k) {    int x = INT8_MAX;    if (k

Find minimum difference between any two element in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:12:02

553 Views

Suppose we have an array of n elements called A. We have to find the minimum difference between any two elements in that array. Suppose the A = [30, 5, 20, 9], then the result will be 4. this is the minimum distance of elements 5 and 9.To solve this problem, we have to follow these steps −Sort the array in non-decreasing orderInitialize the difference as infiniteCompare all adjacent pairs in the sorted array and keep track of the minimum oneExample#include #include using namespace std; int getMinimumDifference(int a[], int n) {    sort(a, a+n);    int min_diff = INT_MAX;    for (int i=0; i

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

Ayush Gupta
Updated on 19-Dec-2019 10:17:18

154 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 Live Demo#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

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

Arnab Chakraborty
Updated on 19-Dec-2019 10:10:25

334 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 Live Demo#include using namespace std; int max_product(int N) {    if (N == 0) ... Read More

Program to multiply two matrices in C++

Ayush Gupta
Updated on 19-Dec-2019 10:08:51

384 Views

In this tutorial, we will be discussing a program to multiply two matrices.For this we will be given with two matrices and our task is to print the product of two those matrices. The only condition is that the number of columns of first matrix should be equal to the number of rows of the second matrix.Example Live Demo#include using namespace std; #define N 4 //multiplying the elements of both matrices void calc_product(int mat1[][N], int mat2[][N], int res[][N]){    int i, j, k;    for (i = 0; i < N; i++) {       for (j = 0; ... Read More

Find maximum level product in Binary Tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:07:25

78 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 Live Demo#include #include using namespace std; class ... Read More

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

Arnab Chakraborty
Updated on 19-Dec-2019 10:04:04

1K+ 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

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

Ayush Gupta
Updated on 19-Dec-2019 10:05:50

2K+ 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 Live Demo#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

Find Length of a Linked List (Iterative and Recursive) in C++

Arnab Chakraborty
Updated on 19-Dec-2019 10:01:36

575 Views

Here we will see how to find the length of a linked list using iterative and recursive approaches. If the head pointer is given, we have to follow these steps to get the length.For iterative approach −Take the head of the list, until the current pointer is not null, go to the next node and increase the count.For recursive approach −Pass head as an argument, the base condition is when the argument is null, then return 0, otherwise recursively enter into the list and send the next node from the current node, return 1 + length of the sublistExample Live Demo#include ... Read More

Program to invert bits of a number Efficiently in C++

Ayush Gupta
Updated on 19-Dec-2019 10:02:48

282 Views

In this tutorial, we will be discussing a program to invert bits of a number efficiently.For this we will be given with a non-negative number. Our task is to convert the number in the binary format, invert the binary bits of the number. And then finally print the decimal equivalent of the number.Example Live Demo#include using namespace std; //inverting bits of number int invert_bit(int n){    int x = log2(n) ;    int m = 1

Advertisements