# Category Archives: Greedy

## Construct a string that has exactly K subsequences from given string

Given a string str and an integer K, the task is to find a string S such that it has exactly K subsequences of given… Read More »

## Replace each element of Array with it’s corresponding rank

Given an array arr[] of N integers, the task is to replace each element of Array with their rank in array.   The rank of an… Read More »

## Sum of first N natural numbers with all powers of 2 added twice

Given an integer N, the task is to calculate the sum of first N natural numbers adding all powers of 2 twice to the sum.Examples: … Read More »

## Largest subset with M as smallest missing number

Given an array arr[] of N positive integers and a positive integer M, the task is to find the length of longest subset whose smallest… Read More »

## Check if a decreasing Array can be sorted using Triple cyclic shift

Given an arr[] of size N whose elements are sorted in descending order. The task is to find if the given array can be sorted… Read More »

## Rearrange an array such that product of every two consecutive elements is a multiple of 4

Given an array arr[] of size N, the task is to rearrange the array elements such that for every index i(1 <= i <= N… Read More »

## Longest substring that starts with X and ends with Y

Given a string str, two characters X and Y. The task is to find the length of the longest substring that starts with X and… Read More »

## Check if a string can be split into two strings with same number of K-frequent characters

Given a string S and an integer K, the task is to check if it is possible to distribute these characters into two strings such… Read More »

## Split array into K subsets to maximize their sum of maximums and minimums

Given an integer K and an array A[ ] whose length is multiple of K, the task is to split the elements of the given… Read More »

## Count of ways to generate a Matrix with product of each row and column as 1 or -1

Given two integers N and M, the task is to find the numbers of ways to form a matrix of size N * M consisting… Read More »

## Min steps to empty an Array by removing a pair each time with sum at most K

Given an array arr[] and a target value K. The task is to find the minimum number of steps required to take all elements from… Read More »

## Longest subarray of non-empty cells after removal of at most a single empty cell

Given a binary array arr[], the task is to find the longest subarray of non-empty cells after the removal of at most 1 empty cell. … Read More »

## Construct the smallest possible Array with given Sum and XOR

Given two positive integers S and X which represents the sum and Bitwise XOR of all the elements of an array arr[]. The task is… Read More »

## Select K elements from an array whose maximum value is minimized

Given an array arr[] having N integers and an integer K, the task is to select K elements from the given array such that sum… Read More »

## Smallest number greater than X which is K-periodic

Given a string integer X consisting of N digits and an integer K, the task is to find the smallest integer greater than or equal… Read More »