# Category Archives: Greedy

## Kruskal’s Algorithm (Simple Implementation for Adjacency Matrix)

Below are the steps for finding MST using Kruskal’s algorithm 1. Sort all the edges in non-decreasing order of their weight. 2. Pick the smallest… Read More »

## Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation)

We have discussed Prim’s algorithm and its implementation for adjacency matrix representation of graphs. As discussed in the previous post, in Prim’s algorithm, two sets… Read More »

## Number of chocolates left after k iterations

Given a pile of chocolates and an integer ‘k’ i.e. the number of iterations, the task is to find the number of chocolates left after… Read More »

## Subarray whose absolute sum is closest to K

Given an array of n elements and an integer K, the task is to find the subarray with minimum value of ||a[i] + a[i +… Read More »

## Length and Breadth of rectangle such that ratio of Area to diagonal^2 is maximum

Given an array of positive integers. The task is to choose a pair of elements from the given array such that they represent the length… Read More »

## Longest subsequence whose average is less than K

Given an array of N positive integers and Q queries consisting of an integer K, the task is to print the length of the longest… Read More »

## Smallest sum contiguous subarray | Set-2

Given an array containing N integers. The task is to find the sum of the elements of the contiguous subarray having the smallest(minimum) sum. Examples:… Read More »

## Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array

Given two arrays of size N, and two numbers X and Y, the task is to maximize the sum by considering the below points: Pick… Read More »

## Sum of minimum difference between consecutive elements of an array

Given an array of pairs where each pair represents a range, the task is to find the sum of the minimum difference between the consecutive… Read More »

## Place N^2 numbers in matrix such that every row has an equal sum

Given a number N, place numbers from the range [1, N2] in an NxN matrix such that sum in every row is equal. Examples: Input:… Read More »

## Water drop problem

Consider a pipe of length L. The pipe has N water droplets at N different positions within it. Each water droplet is moving towards the… Read More »

## Find k pairs with smallest sums in two arrays | Set 2

Given two arrays arr1[] and arr2[] sorted in ascending order and an integer K. The task is to find k pairs with smallest sums such… Read More »

## Maximum number by concatenating every element in a rotation of an array

Given an array of N elements. The task is to print the maximum number by concatenating every element in each rotation. In every rotation, the… Read More »

## Minimum number of operations to convert a given sequence into a Geometric Progression

Given a sequence of N elements, only three operations can be performed on any element at most one time. The operations are: Add one to… Read More »

## Minimum number of adjacent swaps for arranging similar elements together

Given an array of 2 * N positive integers where each array element lies between 1 to N and appears exactly twice in the array.… Read More »