# Category Archives: Greedy

## Maximize number of elements from Array with sum at most K

Given an array A[] of N integers and an integer K, the task is to select maximum number of elements from the array whose sum… Read More »

## Pair of integers with difference K having an element as the K-th multiple of the other

Given an integer K, the task is to find a pair of numbers (A, B) such that A – B = K and A /… Read More »

## Find a number M < N such that difference between their XOR and AND is maximum

Given a natural number N, the task is to find a number M smaller than N such that the difference between their bitwise XOR (N… Read More »

## Sum of largest divisor of numbers upto N not divisible by given prime number P

Given a number N and a prime number P, the task is to find the sum of largest divisors of each number in the range… Read More »

## Number of containers that can be filled in the given time

Given a number N and a time X unit, the task is to find the number of containers that are filled completely in X unit… Read More »

## Count of Binary Strings possible as per given conditions

Given two integers N and M, where N denotes the count of ‘0’ and M denotes the count of ‘1’, and an integer K, the… Read More »

## Remove the first and last occurrence of a given Character from a String

Given a character C and a string S, the task is to remove the first and last occurrence of the character C from the string… Read More »

## Generate a Binary String without any consecutive 0’s and at most K consecutive 1’s

Given two integers N and M, the task is to construct a binary string with the following conditions :  The Binary String consists of N… Read More »

## Derive a MultiSet from given Array such that sum is > P and removing any element makes sum < P

Given an array arr[] of N elements, the task is to derive a MultiSet having the numbers from the given array in possible repetitions, such… Read More »

## Construct a Matrix whose sum of diagonals for each square submatrix is even

Given an integer N, the task is to construct a matrix M[][] of size N x N with numbers in range [1, N^2], such that… Read More »

## Largest possible Subset from an Array such that no element is K times any other element in the Subset

Given an array arr[] consisting of N distinct integers and an integer K, the task is to find the maximum size of a subset possible… Read More »

## Rabin-Karp algorithm for Pattern Searching in Matrix

Given matrices txt[][] of dimensions m1 x m2 and pattern pat[][] of dimensions n1 x n2, the task is to check whether a pattern exists… Read More »

## Change the given string according to the given conditions

Given a string S, the task is to change the string id it doesn’t follow any of the rules given below and print the updated… Read More »

## Check if permutaion of one string can break permutation of another

Given two strings str1 and str2, the task is to check if any permutation of the given strings str1 and str2 is possible such that… Read More »

## Minimum number of integers required such that each Segment contains at least one of them

Given two arrays start[] and end[] consisting of positive integers denoting the starting and ending points of a segment respectively, the task is to find… Read More »