Found 26504 Articles for Server Side Programming

Find number of cavities in a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:39:31

137 Views

Consider one matrix is given. We have to find the number of cavities in the matrix. One element is denoted as cavity when all other elements surrounding it is greater than the element. So if the matrix is like −456715456So the output is 1.We simply check the surrounding elements and form the decision.Example#include #define MAX 100 using namespace std; int numberOfCavities(int array[][MAX], int n) {    int arr[n + 2][n + 2];    int count = 0;    for (int i = 0; i < n + 2; i++) {       for (int j = 0; j < ... Read More

Find the longest common prefix between two strings after performing swaps on second string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:35:42

323 Views

Suppose we have two strings str1 and str2. Find the longest common prefix between them after performing zero or more operation on the second string. In each operation, we can swap any two letters. So if str1 = “HERE”, str2 = “THERE”, then output will be 4. The second string can be made “HERET” by just swapping the characters. So the longest prefix is of length 4.As we know that we can only swap on str2. And the length of the matrix should be maximized. So the idea is to traverse str1, and check if the frequency of the current ... Read More

Find number from given list for which value of the function is closest to A in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:33:32

145 Views

Suppose we have a function F(n) such that F(n) = P – (0.006*n), where P is also given. Given a list of integers and a number A. The task is to find the number from given list, for which the value of the function is nearer to A. So if P = 12, and A = 5, then list will be {1000, 2000} So output will be 1000. So if P = 12 and A = 5, then for 1000, F(1000) = 12 – (0.006 * 1000) = 6 and for 2000, F(2000) = 12 – (0.006 * 2000) = ... Read More

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

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

597 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

234 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

313 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

185 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

204 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

136 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

212 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

Advertisements