Found 26504 Articles for Server Side Programming

Find the number closest to n and divisible by m in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:21:17

2K+ Views

Suppose we have two integers n and m. We have to find the number closest to n and divide by m. If there are more than one such number, then show the number which has maximum absolute value. If n is completely divisible by m, then return n. So if n = 13, m = 4, then output is 12.To solve this, we can follow this steps −let q := n/m, and n1 := m*qif n * m > 0, then n2 := m * (q + 1), otherwise n2 := m * (q - 1)if |n – n1| < ... Read More

Find closest smaller value for every element in array in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:12:12

198 Views

Here we will see how to find the closest value for every element in an array. If an element x has the next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the settings in C++ STL. The set ... Read More

Find smallest number K such that K % p = 0 and q % K = 0 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:10:47

107 Views

Suppose we have two integers P and Q. We have to find smallest number K, such that K mod P = 0 and Q mod K = 0. Otherwise print -1. So if the P and Q are 2 and 8, then K will be 2. As 2 mod 2 = 0, and 8 mode 2 = 0.In order for K to be possible, Q must be divisible by P. So if P mod Q = 0 then print P otherwise print -1.Example Live Demo#include using namespace std; int getMinK(int p, int q) {    if (q % p == 0)    return p;    return -1; } int main() {    int p = 24, q = 48;    cout

Find Selling Price from given Profit Percentage and Cost in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:09:23

276 Views

Consider we have the selling price, and percentage of profit or loss is given. We have to find the cost price of the product. The formula is like below −$$Cost Price=\frac{Sell Price∗100}{100+percentage profit}$$ $$Cost Price=\frac{Sell Price∗100}{100+percentage loss}$$Example Live Demo#include using namespace std; float priceWhenProfit(int sellPrice, int profit) {    return (sellPrice * 100.0) / (100 + profit); } float priceWhenLoss(int sellPrice, int loss) {    return (sellPrice * 100.0) / (100 - loss); } int main() {    int SP, profit, loss;    SP = 1020;    profit = 20;    cout

Find safe cells in a matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:07:14

161 Views

Suppose we have a matrix mat[][]. It has character Z and P. The Z is the zombie and P is the plant. And another character * is a bare land. A zombie can attack the plant, when plant is adjacent to zombie. We have to find number of plants, that are safe from zombie. Suppose the matrix is like below −So there are only two plants that are safe.We will traverse the matrix element by element, then when the current element is a plant, then check that the plant is surrounded by zombie or not, if not then increase the ... Read More

Find repeated character present first in a string in C++

Arnab Chakraborty
Updated on 18-Dec-2019 13:03:35

424 Views

Suppose we have a string; we have to find first character that is repeated. So is the string is “Hello Friends”, the first repeated character will be l. As there are two l’s one after another.To solve this, we will use the hashing technique. Create one hash table, scan each character one by one, if the character is not present, then insert into hash table, if it is already present, then return that character.Example Live Demo#include #include using namespace std; char getFirstRepeatingChar(string &s) {    unordered_set hash;    for (int i=0; i

Find product of prime numbers between 1 to n in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:59:05

360 Views

Suppose we have a number n. We have to find the product of prime numbers between 1 to n. So if n = 7, then output will be 210, as 2 * 3 * 5 * 7 = 210.We will use the Sieve of Eratosthenes method to find all primes. Then calculate the product of them.Example Live Demo#include using namespace std; long PrimeProds(int n) {    bool prime[n + 1];    for(int i = 0; i

Find prime number K in an array such that (A[i] % K) is maximum in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:56:56

128 Views

Suppose we have an array A with n integers. We have to find one element K such that K is prime, and A[i] mod K is maximum for all valid i among all possible values of K. If no such numbers are found, then return -1. For example, if A = [2, 10, 15, 7, 6, 8, 13], then the output will be 13. There are three prime numbers 2, 7, and 13. The maximum possible values of A[i] mod 2 is 1, (15 mod 2), for 7, it will be 6 mod 7 = 6, and for 13, it ... Read More

Find permutation of n which is divisible by 3 but not divisible by 6 in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:53:38

116 Views

Suppose we have a number n, and we have to find the permutation of this number, that is divisible by 3, but not divisible by 6. If no such value can be made, then return -1. For example, if n is 336, then the output can be 363.As we know a number is divisible by 6 means it is divisible by 3 and 2. So each even number that is divisible by 3, will be divisible by 6. If we interchange the digits of a number which is divisible by 3 and also even, to make it odd, it will ... Read More

Find permutation of first N natural numbers that satisfies the given condition in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:52:46

227 Views

Suppose we have two integers N and K, and we have to find the permutation P of first N natural numbers such that there are exactly K elements which satisfies the condition GCD(P[i], i) > 1 for all 1

Advertisements