Found 7347 Articles for C++

Find nth number that contains the digit k or divisible by k in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:30:28

489 Views

Given two positive integers n and k, and we have to find the nth number that contains the digit k or divisible by k. The k will be in range [2 to 9]. So if n and k are 15 and 3 respectively, then output is 33. As the numbers [3, 6, 9, 12, 13, 15, 18, 21, 23, 24, 27, 30, 31, 33] These are those numbers where each element contains the digit k = 3 or divisibility by k and in this nth number is 33. So output is 33.Check each number that contains k and multiple of ... Read More

Find the last non repeating character in string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:39:55

194 Views

Suppose we have a string str. We have to find the last non-repeating character in it. So if the input string is like “programming”. So the first non-repeating character is ‘n’. If no such character is present, then return -1.We can solve this by making one frequency array. This will store the frequency of each of the character of the given string. Once the frequency has been updated, then start traversing the string from the last character one by one. Then check whether the stored frequency is 1 or not, if 1, then return, otherwise go for previous character.Example#include ... Read More

Find the largest number that can be formed with the given digits in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:37:32

225 Views

Suppose we have an array of digits. We have to find the maximum number that can be obtained using all digits of the array. So if the array is like [3, 3, 9, 6, 2, 5], then maximum number can be 965332.From the problem, we can see that we can easily sort the digits in non-increasing order, then print them. But we can solve this using more efficient way. We can create one array of size 10 to store the frequency of each digit, then print the numbers from 9 to 0 accordingly.Example Live Demo#include #include using namespace std; ... Read More

Find minimum x such that (x % k) * (x / k) == n in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:38:56

130 Views

Given two positive integers n and k, and we have to find the positive integer x, such that (x % k)*(x / k) is same as n. So if the n and k are 4 and 6 respectively, then the output will be 10. So (10 % 6) * (10 / 6) = 4.As we know that the value of x % k will be in range [1 to k – 1] (0 is not included) Here we will find possible integer in the range that divides n and hence the given equation becomes: x = (n * k) / ... Read More

Find the largest interval that contains exactly one of the given N integers In C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:35:57

130 Views

Suppose we have an array of N distinct integers. We have to find the max element in an interval [L, R] such that the interval contains exactly one of the given N integers and 1

Find the kth node in vertical order traversal of a Binary Tree in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:33:45

90 Views

Suppose we have a binary tree and a value K. The task is to print the Kth node in the vertical order traversal. If no such node exists, then return -1. So if the tree is like below −The vertical order traversal is like −4 2 1 5 6 3 8 7 9So if K = 3, then result will be 1.The approach is simple. We will perform the vertical order traversal, then check the current node is the kth node or not, if so then return.Example Live Demo#include #include #include #include using namespace std; class Node {    public:   ... Read More

Find the first natural number whose factorial is divisible by x in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:45:33

158 Views

We have to find the first natural number whose factorial is divisible by x. The x is given by the user. So if the x = 16, then output will be 6. as 6! mod 16 = 0. We will use general approach to solve this problem. iteratively count 1!, 2!, …. n! and check divisibility using x. If modulus is 0, then stop and return the number.Example Live Demo#include using namespace std; int getNumber(int x) {    int fact = 1;    int i = 0;    while(fact % x != 0){       i++;       fact = fact * i;    }    return i; } int main() {    int x = 16;    cout

Find minimum steps required to reach the end of a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:35:01

318 Views

Suppose we have a 2D matrix with positive integers. We have to find the minimum steps required to move from to the end of the matrix (rightmost bottom cell), If we are at cell (i, j), we can go to the cell (i, j+mat[i, j]) or (i+mat[i, j], j), We cannot cross the bounds. So if the matrix is like −212111111The output will be 2. Path will be (0, 0) →(0, 2) → (2, 2)Here we will use the Dynamic programming approach to solve this. Suppose we are at cell (i, j), we want to reach (n-1, n-1) cell. We ... Read More

Find minimum radius such that atleast k point lie inside the circle in C++

Arnab Chakraborty
Updated on 18-Dec-2019 11:27:19

260 Views

Suppose we have some points, and one integer k. We have to find minimum radius of a circle whose center is at (0, 0) to cover k points. So if the points are like (1, 1), (-1, -1), (1, -1), and k = 3, then radius will be 2.Here we will find the Euclidean distance between each point and (0, 0), then sort the distances and return the kth element after sorting.Example Live Demo#include #include using namespace std; struct point{    int x, y; }; int minRadius(int k, point points[], int n) {    int dist[n];    for (int i = ... Read More

Find the first, second and third minimum elements in an array in C++ program

Arnab Chakraborty
Updated on 18-Dec-2019 11:25:51

436 Views

Suppose we have an array of n elements. We have to find the first, second and the third minimum elements in the array. First minimum is the minimum of the array, second min is minimum but larger than the first one, and similarly the third min is minimum but greater than second min.Scan through each element, then check the element, and relate the condition for first, second and third min elements conditions to solve this problem.Example#include using namespace std; int getThreeMins(int arr[], int n) {    int first = INT_MAX, sec = INT_MAX, third = INT_MAX;    for (int i ... Read More

Advertisements