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…

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…

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

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…