Found 7347 Articles for C++

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

165 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

132 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

88 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

168 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

73 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

103 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

C++ program to find largest or equal number of A whose sum of digits is divisible by 4

Arnab Chakraborty
Updated on 03-Mar-2022 05:39:41

111 Views

Suppose we have a number A. We have to find nearest larger or equal interesting number for A. A number is said to be interesting number if its sum of digits is divisible by 4. So, if the input is like A = 432, then the output will be 435, because 4 + 3 + 5 = 12 which is divisible by 4.StepsTo solve this, we will follow these steps −while (A / 1000 + A mod 1000 / 100 + A mod 100 / 10 + A mod 10) mod 4 is not equal to 0, do:    (increase A ... Read More

C++ program to find minimum number of locations to place bombs to kill all monsters

Arnab Chakraborty
Updated on 02-Mar-2022 12:26:48

213 Views

Suppose we have two arrays X and H. Both are with N elements, also have another two numbers D and A. Consider in a story, a silver fox is fighting with N monsters. The monsters are standing in a row, coordinate of ith monster is X[i], and its health is H[i]. Silver fox can use bombs to attack monsters. Dropping bomb at location x will damage all monsters within the range of x - D to x + D. Their health will be reduced by A. When all monsters have 0 health left, then the fox wins. We have to ... Read More

C++ program to count maximum possible division can be made in a graph with given condition

Arnab Chakraborty
Updated on 02-Mar-2022 12:21:17

100 Views

Suppose we have an adjacency matrix of a graph G. We have to check whether we can divide the vertices into non-empty sets V1, ... Vk, such that: every edge connects two vertices belonging to two adjacent sets. If the answer is yes, we have to find the maximum possible value of sets k, in such division.So, if the input is like010110101001010100101000100000010000then the output will be 4StepsTo solve this, we will follow these steps −Define an array dp of size: 210. n := size of matrix fl := 1 ans := 0 for initialize i := 0, when i < ... Read More

C++ program to find at least how much score it needs to get G amount of score

Arnab Chakraborty
Updated on 02-Mar-2022 12:24:07

216 Views

Suppose we have two arrays p and c both are with D number of elements each, and another number G. Consider in a coding contest, each problem has its score based on difficulty. The problem p[i] has score 100i. These p[1] + ... + p[D] problems are all of the problems present in the contest. a user in the coding site has a number total_score. The total_score of an user is the sum of the following two elements.Base score: the sum of score of all solved problemsBonus: when a user solves all problems with a score of 100i, he or ... Read More

Advertisements