Found 7197 Articles for C++

C++ program to find sequence of indices of the team members

Arnab Chakraborty
Updated on 03-Mar-2022 10:27:04

199 Views

Suppose we have an array A with n elements and a number k. There are n students in a class. The rating of ith student is A[i]. We have to form a team with k students such that rating of all team members are distinct. If not possible return "Impossible", otherwise return the sequence of indices.So, if the input is like A = [15, 13, 15, 15, 12]; k = 3, then the output will be [1, 2, 5].StepsTo solve this, we will follow these steps −Define two large arrays app and ans and fill them with cnt := 0 ... Read More

C++ program to count minimum number of operations needed to make number n to 1

Arnab Chakraborty
Updated on 03-Mar-2022 10:20:34

377 Views

Suppose we have a number n. We perform any one of these operations arbitrary number of times −Replace n with n/2 when n is divisible by 2Replace n with 2n/3 when n is divisible by 3Replace n with 4n/5 when n is divisible by 5We have to count minimum number of moves needed to make number 1. If not possible, return -1.So, if the input is like n = 10, then the output will be 4, because use n/2 to get 5, then 4n/5 to get 4, then n/2 again to get 2 and n/2 again to get 1.StepsTo solve ... Read More

C++ program to find reduced size of the array after removal operations

Arnab Chakraborty
Updated on 03-Mar-2022 10:13:13

182 Views

Suppose we have an array A with n elements. Consider there is a password with n positive integers. We apply the following operations on the array. The operations is to remove two adjacent elements that are not same as each other, then put their sum at that location. So this operation will reduce the size of array by 1. We have to find the shortest possible length of the array after performing these operations.So, if the input is like A = [2, 1, 3, 1], then the output will be 1, because if we select (1, 3), the array will ... Read More

C++ program to find minimum how many operations needed to make number 0

Arnab Chakraborty
Updated on 03-Mar-2022 10:09:06

299 Views

Suppose we have a numeric string S with n digits. Consider S represents a digital clock and whole string shows an integer from 0 to 10^n - 1. If there are a smaller number of digits, it will show leading 0s. Follow the operations −Decrease the number on clock by 1, orSwap two digitsWe want the clock will show 0 with minimum number of operations needed. We have to count the number of operations needed to do that.So, if the input is like S = "1000", then the output will be 2, because we can swap first 1 with last ... Read More

C++ program to find array after inserting new elements where any two elements difference is in array

Arnab Chakraborty
Updated on 03-Mar-2022 10:05:15

164 Views

Suppose we have an array A with n distinct elements. An array B is called nice if for any two distinct elements B[i] and B[j], the |B[i] - B[j]| appears in B at least once, and all elements in B will be distinct. We have to check whether we can add several integers in A to make it nice of size at most 300. If possible return the new array, otherwise return -1.So, if the input is like A = [4, 8, 12, 6], then the output will be [8, 12, 6, 2, 4, 10], because |4−2| = |6−4| = ... Read More

C++ program to check whether given string is bad or not

Arnab Chakraborty
Updated on 03-Mar-2022 09:57:18

331 Views

Suppose we have a string S with n characters. S contains lowercase English letters and ')' characters. The string is bad, if the number of characters ')' at the end is strictly greater than the number of remaining characters. We have to check whether S is bad or not.So, if the input is like S = "fega))))))", then the output will be True, because this is bad as there are 4 letters and 6 ')'s.StepsTo solve this, we will follow these steps −ans := 0 n := size of S i := n - 1 while (i >= 0 and ... Read More

C++ program to find minimum difference between the sums of two subsets from first n natural numbers

Arnab Chakraborty
Updated on 03-Mar-2022 08:12:37

227 Views

Suppose we have a number n. Consider, first n natural numbers. We have to split them into two sets A and B such that each element belongs to exactly one set and the absolute difference between the sum of elements in A and sum of elements in B is minimum, and find that difference.So, if the input is like n = 5, then the output will be 1, because if we make A = {1, 3, 4} and B = {2, 5}, then the sum values are 8 and 7, so difference is 1.StepsTo solve this, we will follow these ... Read More

C++ program to count number of stairways and number of steps in each stairways

Arnab Chakraborty
Updated on 03-Mar-2022 08:08:46

316 Views

Suppose we have an array A with n elements. Let, Amal climbs the stairs inside a multi-storey building. Every time he climbs, start counting from 1. For example, if he climbs two stairways with 3 steps and 4 steps, he will speak the numbers like 1, 2, 3, 1, 2, 3, 4. In the array A, the numbers are representing stair numbers said by Amal. We have to count the number of staircase did he climb, also print the number of steps in each stairway.So, if the input is like A = [1, 2, 3, 1, 2, 3, 4, 5], ... Read More

C++ program to find indices of soldiers who can form reconnaissance unit

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

321 Views

Suppose we have an array A with n elements. There are n soldiers standing on a circle. For ith soldier, the height is A[i]. A reconnaissance unit can be made of such two adjacent soldiers, whose heights difference is minimal. So each of them will be less noticeable with the other. We have to find the indices of the pair of soldiers that can form a reconnaissance unit.So, if the input is like A = [10, 12, 13, 15, 10], then the output will be (5, 1).StepsTo solve this, we will follow these steps −n := size of A D ... Read More

C++ program to check xor game results 0 or not

Arnab Chakraborty
Updated on 03-Mar-2022 07:56:00

174 Views

Suppose we have an array A with N elements and another binary string S. Consider two players are playing a game. They are numbered as 0 and 1. There is one variable x whose initial value is 0. The games has N rounds. In ith round person S[i] does one of the following: replace x with x XOR A[i], otherwise do nothing. Person 0 wants 0 at the end of this game but person 1 wants non-zero. We have to check whether x becomes 0 at the end or not.So, if the input is like A = [1, 2]; S ... Read More

Advertisements