Found 7197 Articles for C++

Minimum number of moves to make all elements equal using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:12:57

263 Views

Problem statementGiven an array of N elements and an integer K., It is allowed to perform the following operation any number of times on the given array −Insert the Kth element at the end of the array and delete the first element of the array.The task is to find the minimum number of moves needed to make all elements of the array equal. Print -1 if it is not possibleIf arr[] = {1, 2, 3, 4, 5, 6} and k = 6 then minimum 5 moves are required: Move-1: {2, 3, 4, 5, 6, 6} Move-2: {3, 4, 5, 6, ... Read More

Minimum number of mails required to distribute all the questions using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:04:38

117 Views

Problem statementGiven N questions in a test and K students in the class. Out of the batch of K students, N students memorized exactly one question each. A mail can contain about a maximum of X questions.Find the minimum number of mails required so that the entire class gets to know about all the questionsIf N = 3, K = 3, X = 1 then one has to send 6 mails −Student 1 sends his question to student 2 and student 3 (2 mails), So does student 2 and student 3 so total mails = 2 * 3 = 6AlgorithmThe ... Read More

Minimum number of letters needed to make a total of n in C++.

Narendra Kumar
Updated on 31-Oct-2019 07:02:45

88 Views

Problem statementGiven an integer n and let a = 1, b = 2, c= 3, ….., z = 26. The task is to find the minimum number of letters needed to make a total of nIf n = 23 then output is 1 If n = 72 then output is 3(26 + 26 + 20)Algorithm1. If n is divisible by 26 then answer is (n/26) 2. If n is not divisible by 26 then answer is (n/26) + 1Example#include using namespace std; int minRequiredSets(int n){    if (n % 26 == 0) {       return (n / 26);    } else {       return (n / 26) + 1;    } } int main(){    int n = 72;    cout

Minimum number of items to be delivered using C++.

Narendra Kumar
Updated on 31-Oct-2019 07:00:39

295 Views

Problem statementGiven an array of size, N represents buckets, each array index containing items. Given K tours within which all of the items are needed to be delivered. It is allowed to take items from only one bucket in 1 tour. The task is to tell the minimum number of items needed to be delivered per tour so that all of the items can be delivered within K tours.If there are 5 buckets with item = {1, 3, 5, 7, 9} and 10 tours then we can deliver 3 items per tour By delivering 3 items at a time, 1st ... Read More

Minimum number of given operations required to make two strings equal using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:57:19

546 Views

Problem statementGiven two strings str1 and str2, both strings contain characters ‘a’ and ‘b’. Both strings are of equal lengths. There is one _ (empty space) in both the strings. The task is to convert the first string into the second string by doing the minimum number of the following operations −If _ is at a position I then _ can be swapped with a character at position i+1 or i-1If characters at positions i+1 and i+2 are different then _ can be swapped with a character at position i+1 or i+2Similarly, if characters at positions i-1 and i-2 are ... Read More

Minimum number of given moves required to make N divisible by 25 using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:52:07

359 Views

Problem statementGiven a number N without leading zeros. The task is to find the minimum number of moves required to make N divisible by 25. At each move, one can swap any two adjacent digits and make sure that at any time number must not contain any leading zeros. If it is not possible to make N divisible by 25 then print -1If N = 5071 then 4 moves are required to make it divisible by 255071 → 5701 → 7501 → 7510 → 7150Algorithm1. Iterate over all pairs of digits in the number. Let the first digit in the ... Read More

Minimum number of elements to be removed to make XOR maximum using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:48:11

191 Views

Problem statementGiven a number N. The task is to find the minimum number of elements to be removed in between to N such that the XOR obtained from the remaining elements is maximum.Algorithm1. If n is 1 or 2 then there is no need to remove any element. Hence answer is zero 2. Find a number which is power of 2 and greater than or equal to. Let us call this number as nextNumber    2.1. If n == nextNumber or n == (nextNumber – 1) then answer is 1    2.2. If n = (nextNumber -2) then answer is ... Read More

Minimum number of elements to add to make median equals x using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:45:26

232 Views

Problem statementGiven an array “arr” of size n and element x, the task is to find a minimum number of elements to be added in array to make median equals x.A median in an array with the length of n is an element which occupies position number (n-1)/2 after we sort the elements in the non-decreasing order. For example, in below array median is 20 −arr1[] = {10, 20, 30, 40}If arr[] = {1, 2, 3} and x = 4 then we have to add 4 number i.e. {4, 5, 5, 5} in array to make median equal to 4AlgorithmThe ... Read More

Minimum number of elements that should be removed to make the array good using C++.

Narendra Kumar
Updated on 31-Oct-2019 06:43:01

993 Views

Problem statementGiven an array “arr”, the task is to find the minimum number of elements to be removed to make the array good.A sequence a1, a2, a3. . .an is called good if for each element a[i], there exists an element a[j] (i not equals to j) such that a[i] + a[j] is a power of two.arr1[] = {1, 1, 7, 1, 5}In above array if we delete element ‘5’ then array becomes good array. After this any pair of arr[i] + arr[j] is power of two −arr[0] + arr[1] = (1 + 1) = 2 which power of twoarr[0] ... Read More

Minimum number of deletions to make a string palindrome in C++.

Narendra Kumar
Updated on 31-Oct-2019 06:40:01

995 Views

Problem statementGiven a string of size ‘n’. The task is to delete a minimum number of characters to make string palindrome.If the given string is “abcda” then we can delete any 2 characters except first and last to make it a palindrome.If we delete character ‘b’ and ‘c’ then “ada” string is a palindromeIf we delete character ‘c’ and ‘d’ then “aba” string is a palindromeIf we delete character ‘b’ and ‘d’ then “aca” string is a palindromeAlgorithm1. Find longest palindromic subsequence of given string. Let’s call it as “lpsSize”. 2. Minimum characters to be deleted = (length of string ... Read More

Advertisements