Close

## Maximum increasing sub array

Given an array of numbers, how to find the maximum length of increasing (non-decreasing) continuous sub-sequence? For example consider the array [7, 9, 1, 3, 5, 8, 2], the maximum length is 4. Similarly for [23, 10, 18, 18, 6], it is 3. This is a straight forward implementation problem (appeared on Codeforces). Following is…

## Check if an array has a sub-array with given sum

Given an array of numbers and a sum, how to check if there is any sub-array with the given sum. For example consider an array A = [7, 2, 9, 1, 5], and the sum is 12, then we can find a sub-array [2, 9, 1] which sums up to 12. So the answer is…

## Decreasing string

Given a number K, Find the shortest and smallest string  which has K positions in it, such that the character at that position is alphabetically greater than the character immediately after it. Only the lower case alphabets [a-z] are allowed. For example consider K = 1 “ba” is the shortest and smallest string possible. There…

## Up down array

Given an array of numbers A[N], how do we arrange them in the following order. A[0] <= A[1] >= A[2] <= A[3]…A[N-1] For example consider the array A =[6, 9, 36, 24, 42] The answer can be [6, 36, 9, 42, 24]. There can be multiple answers also like [6, 42, 9, 36, 24]. We…

## 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…

## 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…

## XOR of all sub arrays

Given an array of elements, how do we find the XOR of each sub-array and XOR of those results? For example let us consider the array [1, 2, 3], all possible sub-arrays are XOR[1] = 1XOR[1, 2] = 1 ^ 2 = 3XOR[1, 2, 3] = 1 ^ 2 ^ 3 = 0XOR[2] = 2XOR[2,…

## Character to be removed to become a palindrome

Given a string, We have to find out by removing which character, it becomes a palindrome.For example consider the string “racercar”, the character ‘r’ at index 4 should be removed to become a palindrome.The approach here is simple. Start from both ends of the string and compare both characters. If they are equal, we increment…

## How does a radix sort work?

Radix sort is an integer sorting algorithm. As it’s name indicates, it sorts a list of numbers based on the digits of the numbers.  It’s a linear time sorting algorithm which is different from the comparison based sorting algorithms like quick sort or heap sort. Counting sort is also a linear time sorting algorithm which…