Found 7347 Articles for C++

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

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

378 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

54 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

97 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

123 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

177 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

142 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

319 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

126 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

Minimum number of single digit primes required whose sum is equal to N in C++

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

146 Views

Problem statementFind the minimum number of single-digit prime numbers required whose sum will be equal to N.ExampleIf N = 9 then we require 2 prime numbers i.e. 7 and 2 to make sum 9.Example#include using namespace std; bool isValidIndex(int i, int val) {    return (i - val) < 0 ? false : true; } int getMinPrimes(int n) {    int arr[n + 1];    for (int i = 1; i

Minimum number of sets with numbers less than Y in C++

Narendra Kumar
Updated on 22-Nov-2019 10:43:13

385 Views

Problem statementGiven a string of consecutive digits and a number Y, the task is to find the number of minimum sets such that every set follows the below rule −Set should contain consecutive numbersNo digit can be used more than once.The number in the set should not be more than Y.ExampleIf str = “1234” and Y = 20 then answer is 3 as below sets are created −{12} {3} and {4}AlgorithmConvert string to numberIf the number is not greater than Y, then mark f = 1If the number exceeds Y, then increase count if f = 1 and re-initialize f ... Read More

Advertisements