Category Archives: Algorithm

Max Flow Problem Introduction

Maximum flow problems involve finding a feasible flow through a single-source, single-sink flow network that is maximum. Let’s take an image to explain how above definition wants to say. Each edge is labeled with a capacity, the maximum amount of stuff that it can carry. The goal is to figure out how much stuff can… Read More »

N-th root of a number

Given two numbers N and A, find N-th root of A. In mathematics, Nth root of a number A is a real number that gives A, when we raise it to integer power N. These roots are used in Number Theory and other advanced branches of mathematics. Refer Wiki page for more information. Examples: Input… Read More »

Printing all solutions in N-Queen Problem

The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem. The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each… Read More »

Largest subarray with GCD one

There is an array with n elements. Find length of the largest subarray having GCD equal to 1. If no subarray with GCD 1, then print -1. Examples: Input : 1 3 5 Output : 3 Input : 2 4 6 Output :-1 A simple solution is to consider every subarray and find its GCD… Read More »

Reverse Delete Algorithm for Minimum Spanning Tree

Reverse Delete algorithm is closely related to Kruskal’s algorithm. In Kruskal’s algorithm what we do is : Sort edges by increasing order of their weights. After sorting, we one by one pick edges in increasing order. We include current picked edge if by including this in spanning tree not form any cycle until there are… Read More »

Friends Pairing Problem

Given n friends, each one can remain single or can be paired up with some other friend. Each friend can be paired only once. Find out the total number of ways in which friends can remain single or can be paired up. Examples: Input : n = 3 Output : 4 Explanation {1}, {2}, {3}… Read More »

Minimum Sum Path In 3-D Array

Given a 3-D array arr[l][m][n], the task is to find the minimum path sum from the first cell of array to the last cell of array. We can only traverse to adjacent element, i.e., from a given cell (i, j, k), cells (i+1, j, k), (i, j+1, k) and (i, j, k+1) can be traversed,… Read More »

Exponential Search

The name of this searching algorithm may be misleading as it works in O(Log n) time. The name comes from the way it searches an element. Given a sorted array an element x to be searched, find position of x in the array. Input: arr[] = {10, 20, 40, 45, 55} x = 45 Output:… Read More »

Bresenham’s Line Generation Algorithm

You are given coordinate of two points A(x1, y1) and B(x2, y2) such that x1 < x2 and y1< y2. You have to find all the intermediate points required for drawing line AB on the computer screen as per Bresenham’s Line Generation Algorithm. Examples: Input : A(0,0), B(4,4) Output : (0,0), (1,1), (2,2), (3,3), (4,4)… Read More »