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
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
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
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
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
Problem statementGiven n strings that are permutations of each other. We need to make all strings same with an operation that moves first character of any string to the end of it.ExampleIf arr[] = {“abcd”, “cdab”} then 2 moves are required.Let us take first string “abcd”. Move character ‘a’ to the end of the string. After this operation string becomes “bcda”Now move character ‘b’ to the end of the string. After this operation string becomes “cdab”. Which in turn makes both strings equalAlgorithmTake first string. Let us call it as ‘str1’.Create a temp string by concating str1 to str1 as ... Read More
Problem statementGiven a binary string in which we can flip all 1’s in left part and all 0’s in right part. The task is to calculate minimum flips required to make all 1’s in left and all 0’s in rightExampleGiven binary string is 0010101. In this string there are 3 1-bits and 4 0-bits. We have to flip highlighted 4 bits to make all 1’s in left and all 0’s in right as shown below −0010101After flipping string will become −1110000AlgorithmTraverse the string from left to right and calculate the number of flips required to convert all 0’s to 1’s.Traverse ... Read More
DescriptionGiven two integers a and b, find the smallest possible height such that a triangle of atleast area ‘a’ and base ‘b’ can be formed.ExampleIf a = 16 and b = 4 then minimum height would be 8AlgorithmArea of triangle can be calculate using below formula −area = ½ * height * baseUsing above formula, height can be calculated as −height = (2 * area) / baseSo Minimum height is the ceil() of the height obtained using above formula.Example#include #include using namespace std; float minHeight(int area, int base) { return ceil((2 * area) / base); } int main() { int area = 16, base = 4; cout
In this problem, we are given a value n, and we want to make change of n rupees, and we have n number of coins each of value ranging from 1 to m. And we have to return the total number of ways in which make the sum.ExampleInput : N = 6 ; coins = {1, 2, 4}. Output : 6 Explanation : The total combination that make the sum of 6 is : {1, 1, 1, 1, 1, 1} ; {1, 1, 1, 1, 2}; {1, 1, 2, 2}; {1, 1, 4}; {2, 2, 2} ; {2, 4}.Example#include ... Read More
Binomial coefficient denoted as c(n, k) or ncr is defined as coefficient of xk in the binomial expansion of (1+X)n.The Binomial coefficient also gives the value of the number of ways in which k items are chosen from among n objects i.e. k-combinations of n-element set. The order of selection of items not considered.Here, we are given two parameters n and k and we have to return the value of binomial coefficient nck .ExampleInput : n = 8 and k = 3 Output : 56There can be multiple solutions to this problem, General SolutionThere is a method to calculate the value ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP