Found 7197 Articles for C++

C++ program to find maximum possible value of XORed sum

Arnab Chakraborty
Updated on 03-Mar-2022 06:03:28

160 Views

Suppose we have an array A with N elements and another value K. For an integer X in range 0 to K, let f(X) = (X xor A[1]) + (X xor A[2]) + ... + (X xor A[N]). We have to find the maximum possible value of f.So, if the input is like K = 7; A = [1, 6, 3], then the output will be 14, because f(4) = (4 XOR 1) + (4 XOR 6) + (4 XOR 3) = 5 + 2 + 7 = 14.StepsTo solve this, we will follow these steps −n := size of ... Read More

C++ program to find minimum number of steps needed to move from start to end

Arnab Chakraborty
Updated on 03-Mar-2022 06:02:02

758 Views

Suppose we have a coordinate (x, y). On a 2D grid, a robot is at (0, 0) position and want to reach (x, y). It can move up, down, left or right or stay at current cell. It wants to reach destination with as minimum as possible commands. We have to count the number of steps needed.So, if the input is like x = 3; y = 4, then the output will be 7StepsTo solve this, we will follow these steps −return x + y + minimum of |x - y|, |x - y + 1|, and |x - y ... Read More

C++ program to count at least how many operations needed for given conditions

Arnab Chakraborty
Updated on 03-Mar-2022 06:00:04

209 Views

Suppose we have an array A with N elements. In each operation, we pick an element and increase or decrease it by 1. We have to find at least how many operations are needed to satisfy following conditions −For every i in range 1 to n, the sum of the terms from 1st through ith term is not 0For every i in range 1 to n - 1, the sign of the terms from the 1st through ith term is different from sign of the sum of the terms from the 1st through (i+1)th term.So, if the input is like ... Read More

C++ program to find smallest and largest number of children in game before start

Arnab Chakraborty
Updated on 03-Mar-2022 05:57:06

201 Views

Suppose we have an array A with K number of elements. Consider, in a game there are N players and a game master is there. This game has K rounds. In ith round game master announces to form groups with A[i] number of children. Then the remaining children form as many groups of A[i] children as possible. One child cannot take part into multiple groups. Those who are left without a group leave the game. The others proceed to the next round. A round may have with no player loss. In the end, after the K-th round, there are exactly ... Read More

C++ program to find permutation for which sum of adjacent elements sort is same as given array

Arnab Chakraborty
Updated on 03-Mar-2022 05:58:50

223 Views

Suppose we have an array A with n elements. A function F(p) is a sorted array of sums of adjacent elements in p. So F(p) = sort([p1 + p2, p2 + p3, ... pn-1 + pn]). We have a permutation represented in A. We have to find the different permutation of A where F(A) is same.So, if the input is like A = [2, 1, 6, 5, 4, 3], then the output will be [1, 2, 5, 6, 3, 4], because F(A)=sort([2+1, 1+6, 6+5, 5+4, 4+3]) = sort([3, 7, 11, 9, 7]) = [3, 7, 7, 9, 11]. And sort([1+2, ... Read More

C++ program to find how many characters should be rearranged to order string in sorted form

Arnab Chakraborty
Updated on 03-Mar-2022 05:53:45

186 Views

Suppose we have a string S with n characters. S contains lowercase letters only. We must select a number k in range 0 to n, then select k characters from S and permute them in any order. In this process, the remaining characters will remain unchanged. We perform this whole operation exactly once. We have to find the value of k, for which S becomes sorted in alphabetical order.So, if the input is like S = "acdb", then the output will be 3, because 'a' is at the correct place and remaining characters should be rearranged.StepsTo solve this, we will ... Read More

C++ program to find number of l-r pairs for which XORed results same as summation

Arnab Chakraborty
Updated on 03-Mar-2022 05:53:54

118 Views

Suppose we have an array A with N elements. We have to find the number of pairs of integers l and r that satisfies A[l] XOR A[l+1] XOR ... XOR A[r-1] XOR A[r] = A[l] + A[l+1] + ... A[r].So, if the input is like A = [2, 5, 4, 6], then the output will be 5, because for pairs (1,1), (2,2), (3,3), (4,4) and (1,2).StepsTo solve this, we will follow these steps −n := size of A Define some arrays of size (n + 1) each, a, s and sx for initialize i := 1, when i

C++ program to count how many minutes we have to wait to meet at least one swimmer

Arnab Chakraborty
Updated on 03-Mar-2022 05:50:08

228 Views

Suppose we have four numbers p, a, b and c. There is a pool and three swimmers are there. They take a, b and c minutes to cross the pool and come back, respectively. So the first swimmer will be at the left side of pool after 0, a, 2a, 3a, ... minutes after the start time. The second will be at 0, b, 2b, 3b, ... minutes and for the third one 0, c, 2c, 3c, ... If we visit the pool after p minutes they have started swimming we have to find how much time we have to ... Read More

C++ program to check we can rearrange array in such a way that given formula returns m or not

Arnab Chakraborty
Updated on 03-Mar-2022 05:43:20

106 Views

Suppose we have an array A with n elements and another number m. We have to check whether we can rearrange the array in such a way that$$\mathrm{\sum_{i=1}^{n} \sum_{j=1}^{n}\frac{A[j]}{j} = m}$$There will be no rounding in A[j]/j operation.So, if the input is like A = [2, 5, 1]; m = 8, then the output will be True, because for the arrangement of [1, 2, 5], (1/1 + 2/2 + 5/3) + (2/2 + 5/3) + (5/3) = 8stepsTo solve this, we will follow these steps −sum := 0 n := size of A for initialize i := 0, when i ... Read More

C++ program to find sum of all cells lighten by the lamps

Arnab Chakraborty
Updated on 03-Mar-2022 05:49:46

177 Views

Suppose we have a grid with H rows and W columns. Where each square is tidy or untidy. We can place lamps on zero or more more tidy squares in this grid. A lamp can lighten the cells in each of the four directions - up, down, left, and right - to the point just before an edge of the grid or an untidy square is reached for the first time (the untidy cell will not be lighted). The lamp will also lighten the cell on which it is placed. In the grid if G[i, j] is '.' that cell ... Read More

Advertisements