Close

## How to check if sequence is a sub sequence of another

Given two arrays of numbers, how to check if the first sequence is a sub sequence of second sequence.For example [3, 7, 11] is a sub-sequence of [9, 3, 1, 7, 5, 11]where as [4, 8, 9] is not a sub-sequence of [6, 4, 9, 2, 8]The algorithm here is simple.  Let us assume two…

## Finding the pivot element

Given an array of numbers ( minimum length 3), find an index such that all the elements to the left of it are less than or equal, and all the elements to the right of it are greater than it.For example in the array [1, 2, 3], the pivot element is 2, as all the…

## Minimum number of squares formed from a rectangle

Given a rectangle of some length and width, How do you divide them into minimum number of squares? This is a problem from Codechef.For example give a rectangle of size 2 x 4, we can make them into two squares of 2 x 2.Similarly a rectangle of size 3 x 7, can only be divided…

## Ambiguous permutations

Given a permutation of numbers from 1 to N, We can represent it in two ways. For example, let us consider a permutation from 1 to 5 P = [2 4 1 5 3] This representation directly indicates the position of each number. i.e 2 is first, 4 is second and so on. Alternatively, this…

## Duplicate elements in array at given distance

Given an array of numbers A, which might contain duplicate elements,  How do we find if there are two elements A[i], A[j] such that i, j are distinct and the difference between them is at most k? Look at the following examples.1. A = [9, 5, 6, 9, 3, 0, 1], K =3The answer is…

## Mirror image of a binary tree

Given a binary tree, how to convert it to it’s mirror image?For example consider the following binary tree and it’s mirror image. Here simply, the left and right sub trees of each node are inverted. So we have to simply invert the given binary tree. It is a simple implementation problem. Using recursion we can…