Minimum operations required to set all elements of binary matrix in C++

Narendra Kumar
Updated on 22-Nov-2019 11:58:52

134 Views

Problem statementGiven a binary matrix of N rows and M columns. The operation allowed on the matrix is to choose any index (x, y) and toggle all the elements between the rectangle having top-left as (0, 0) and bottom-right as (x-1, y-1). Toggling the element means changing 1 to 0 and 0 to 1. The task is to find minimum operations required to make set all the elements of the matrix i.e make all elements as 1.ExampleIf input matrix is {0, 0, 0, 1, 1}    {0, 0, 0, 1, 1}    {0, 0, 0, 1, 1}    {1, 1, ... Read More

Minimum operations to make the MEX of the given set equal to x in C++

Narendra Kumar
Updated on 22-Nov-2019 11:57:49

418 Views

Problem statementGiven a set of n integers, perform minimum number of operations (you can insert/delete elements into/from the set) to make the MEX of the set equal to x (that is given).Note − The MEX of a set of integers is the minimum non-negative integer that doesn’t exist in it. For example, the MEX of the set {0, 2, 4} is 1 and the MEX of the set {1, 2, 3} is 0ExampleIf n = 5 and x = 3 and array is {0, 4, 5, 6, 7} then we require minimum 2 operationsAlgorithmThe approach is to see that in ... Read More

Minimum operations to make XOR of array zero in C++

Narendra Kumar
Updated on 22-Nov-2019 11:57:20

331 Views

Problem statementWe are given an array of n elements. The task is to make XOR of whole array 0. We can do following to achieve this.We can select any one of the element −After selecting element, we can either increment or decrement it by 1.We need to find the minimum number of increment/decrement operation required for the selected element to make the XOR sum of whole array zeroExampleIf arr[] = {2, 4, 7} then 1 operation is required −Select element 2Decrement it by 1Now array becomes {3, 4, 7} and its XOR is 0AlgorithmFind the XOR of whole arrayNow, suppose ... Read More

Minimum partitions of maximum size 2 and sum limited by given value in C++

Narendra Kumar
Updated on 22-Nov-2019 11:24:10

47 Views

Problem statementGiven an array arr[] of positive numbers, find minimum number of sets in array which satisfy following property, A set can contain maximum two elements in it. The two elements need not to be contiguous.Sum of elements of set should be less than or equal to given Key. It may be assumed that given key is greater than or equal to the largest array element.ExampleIf arr[] = {1, 2, 3, 4} and k = 5 then following 2 pairs can be created −{1, 4} and {2, 3}AlgorithmSort the arrayBegin two pointers from two corners of the sorted array. If ... Read More

Minimum Players required to win the game in C++

Narendra Kumar
Updated on 22-Nov-2019 11:19:18

91 Views

Problem statementGiven N questions and K options for each question, where 1

Minimum positive integer value possible of X for given A and B in X = P*A + Q*B in C++

Narendra Kumar
Updated on 22-Nov-2019 11:13:15

112 Views

Problem statementGiven values of A and B, find the minimum positive integer value of X that can be achieved in the equation X = P*A + Q*B, Here P and Q can be zero or any positive or negative integer.ExampleIf A = 2 and B = 4 then answer will be 2.AlgorithmWe need to find P and Q such that P*A > P*B and P*A – P*B is minimum positive integer.This problem can be easily solved by calculating GCD of both numbers)Example#include using namespace std; int getGcd(int a, int b) {    if (a == 0) {     ... Read More

Minimum Possible value of |ai + aj – k| for given array and k in C++

Narendra Kumar
Updated on 22-Nov-2019 11:09:01

166 Views

Problem statementYou are given an array of n integer and an integer K. Find the number of total unordered pairs {i, j} such that absolute value of |ai + aj – k| is minimal possible where i != j.ExampleIf arr[ ] = {0, 4, 6, 2, 4} and k = 7 then we can create following 5 pairs with minimal value as 1{0, 6}, {4, 2}, {4, 4}, {6, 2}, {2, 4}AlgorithmIterate over all possible pairs and for each pair we will check whether the value of (ai + aj – K) is smaller than our current smallest value of ... Read More

Minimum positive integer required to split the array equally in C++

Narendra Kumar
Updated on 22-Nov-2019 11:04:23

124 Views

Problem statementGiven an array of N positive integers, the task is to find the smallest positive integer that can be placed between any two elements of the array such that, the sum of elements in the subarray occurring before it, is equal to the sum of elements occurring in the subarray after it, with the newly placed integer included in either of the two subarraysExampleIf arr = {3, 2, 1, 5, 7, 10} then output is 6. If we place value 6 in between 5 and 7 then sum of left and right subarray becomes equal as follows −+ 2 ... Read More

Minimum possible final health of the last monster in a game in C++

Narendra Kumar
Updated on 22-Nov-2019 10:58:54

312 Views

Problem statementGiven N monsters, each monster has initial health h[i] which is an integer. A monster is alive if its health is greater than 0.In each turn a random monster kills another random monster, the monster which is attacked, its health reduces by the amount of health of the attacking monster. This process is continued until a single monster is left. What will be the minimum possible health of the last remained monster.ExampleIf input array is {2, 14, 28, 56} then output will be 2 because When only the first monster keeps on attacking the remaining 3 monsters, the final ... Read More

Minimum number of Square Free Divisors in C++

Narendra Kumar
Updated on 22-Nov-2019 10:54:48

121 Views

Problem statementGiven an integer N. Find the minimum number of square free divisors.The factorization of N should comprise of only those divisors that are not full squareExampleIf N = 24 then there are 3 square free factors as follows −Factors = 2 * 6 * 2AlgorithmFind all prime factors upto square root of NNow, consider all prime factors less than or equal to square root of N and for each prime factor find its maximum power in number N (like max power of 2 in 24 is 3)Now, we know that if a prime factor has a power greater than ... Read More

Advertisements