Found 7197 Articles for C++

Find the number of binary strings of length N with at least 3 consecutive 1s in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:24:56

377 Views

Suppose, we have an integer N, We have to find the number of all possible distinct binary strings of the length N, which have at least three consecutive 1s. So if n = 4, then the numbers will be 0111, 1110, 1111, so output will be 3.To solve this, we can use the Dynamic programming approach. So DP(i, x) indicates number of strings of length i with x consecutive 1s in position i + 1 to i + x. Then the recurrence relation will be like −DP(i, x) = DP(i – 1, 0) + DP(i – 1, x + 1).The ... Read More

Find common elements in three linked lists in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:23:38

228 Views

Suppose we have three linked lists. We have to find all the common elements that are present in these three linked lists. Suppose these lists are [10, 12, 15, 20, 25], [10, 12, 13, 15] and [10, 12, 15, 24, 25, 26], then the common elements in these three lists are 10, 12 and 15.We will use the hashing technique to solve this problem. To solve this, we have to follow these steps −Create an empty hash table, and go through each element in the first table, and insert the elements, and mark the frequency as 1Iterate through the second ... Read More

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

197 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

106 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

275 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

160 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

127 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

Advertisements