Close

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…

Longest sub array with zero sum

Given an array of integers (positive and negative), how do we find a longest sub-array with zero sum? For example, consider the example [5, 0, -1, 3, -2, 4], the longest sub array with zero sum contains 4 elements.  The straight forward approach is to check the sum of all possible arrays and find out…