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

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

159 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

116 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

309 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

117 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

131 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

372 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

Minimum number of prefix reversals to sort permutation of first N numbers in C++

Narendra Kumar
Updated on 22-Nov-2019 10:37:32

114 Views

DescriptionGiven an array of N numbers which have a permutation of first N numbers. In a single operation, any prefix can be reversed. The task is to find the minimum number of such operations such that the numbers in the array are in sorted in increasing order.ExampleIf array is {1, 2, 4, 3} then minimum 3 steps are required to sort an array in increasing order −Reverse entire array {3, 4, 2, 1}Reverse first two elements {4, 3, 2, 1}Reverse entire array {1, 2, 3, 4}AlgorithmEncode the given numbers in a string. Sort the array and encode it into a ... Read More

Minimum number of Parentheses to be added to make it valid in C++

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

234 Views

Problem statementGiven a string of parentheses. It can container opening parentheses ’(‘ or closing parentheses ‘)’. We have to find minimum number of parentheses to make the resulting parentheses string is valid.ExampleIf str = “((()” then we required 2 closing parentheses i.e ‘))’ at end of stringAlgorithmCount opening parenthesesCount closing parenthesesRequired parentheses = abs(no. of opening parentheses – no. of closing parentheses)Example#include #include #include using namespace std; int requiredParentheses(string str) {    int openingParentheses = 0, closingParentheses = 0;    for (int i = 0; i < str.length(); ++i) {       if (str[i] == '(') ... Read More

Minimum number of bottles required to fill K glasses in C++

Narendra Kumar
Updated on 22-Nov-2019 10:06:15

893 Views

Problem statementGiven N glasses having water, and a list of each of their capacity. The task is to find the minimum number of bottles required to fill out exactly K glasses. The capacity of each bottle is 100 units.ExampleIf N = 5, K = 4, capacity[] = {1, 2, 3, 2, 1}.Filling the glasses with capacities 2, 3, 2, requires 8units.This way, it's enough to open just 1 bottle.AlgorithmTo fill out exactly K glasses, take the K glasses with least capacityTotal required bottles can be calculated as −Ceil value of (Sum of capacities of 1st k glasses) / (Capacity of ... Read More

Minimum number of Appends needed to make a string palindrome in C++

Narendra Kumar
Updated on 22-Nov-2019 10:01:29

283 Views

Problem statementGiven a string, find minimum characters to be appended to make a string palindrome.ExampleIf string is abcac then we can make string palindrome by appending 2 highlighed characters i.e. abcacbaAlgorithmCheck if string is already palindrome, if yes then no need to append any characters.One by one remove a character from string and check whether remaining string is palindrome or notRepeat above process until string becomes palidromeReturn the number of characters removed so far as a final answerExample#include #include using namespace std; bool isPalindrome(char *str) {    int n = strlen(str);    if (n == 1) {   ... Read More

Advertisements