Found 7347 Articles for C++

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

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

199 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

101 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

316 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

294 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

72 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

71 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

155 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

Find the multiple of x which is closest to a^b in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:54:03

93 Views

Suppose we have three integers, a, b and x. The task is to get the multiple of x, which is closest to ab. So if a = 5, b = 4 and x = 3, then output will be 624. As 54 = 625, and 624 is the multiple of 3, which is closest to 625.The task is simple. we have to follow these steps to solve this problem −calculate num := abThen find f := floor of (num/x)Now the closest element at the left will be cl = x * f, and at right will be cr = x ... Read More

Find the missing number in Geometric Progression in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:48:57

90 Views

Suppose we have an array that represents elements of geometric progression in order. One element is missing. We have to find the missing element. So if arr = [1, 3, 27, 81], output is 9, as 9 is missing.Using binary search, we can solve this problem. We will go to the middle element, then check whether the ratio between middle and next to the middle is same as common ratio or not. If not, then missing element is present between indices mid and mid + 1. If the middle element is the n/2th element in the GP, then missing element ... Read More

Find pair with maximum difference in any column of a Matrix in C++

Arnab Chakraborty
Updated on 18-Dec-2019 12:50:47

74 Views

Suppose we have one matrix or order NxN. We have to find a pair of elements which forms maximum difference from any column of the matrix. So if the matrix is like −123535967So output will be 8. As the pair is (1, 9) from column 0.The idea is simple, we have to simply find the difference between max and min elements of each column. Then return max difference.Example#include #define N 5 using namespace std; int maxVal(int x, int y){    return (x > y) ? x : y; } int minVal(int x, int y){    return (x > y) ? ... Read More

Advertisements