Traversing a matrix in spiral order

Given a Matrix, write a function that will print the elements in spiral order. For example, if the Matrix is as below: Then the output should be: 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10

Rotating an array around a pivot

Given an Array of (say) integers like below: 1 2 3 4 5 6 7 8 and an index, say, 2. Write a function which will rotate the array around index 2. i.e the output should be: 3 4 5 6 7 8 1 2

Permutations of a String or Array

Print all possible permutations of an Array or a String. For Example: If the array is arr={1, 2, 3}. Then the possible permutations are: 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1

Minimum path sum in a matrix

Given a Matrix of positive integers and 2 cells A & B. You have to reach from A to B covering cells such that the sum of elements in path is minimum. You can travel in forward, downward and diagonally. For example: If the given Matrix is as below: 1 2 3 4 8 2 […]

Sorting a binary array (which has only 0 & 1)

Given an array which has only 0’s and 1’s. Write an algorithm which will sort this array. (i.e separate 0’s and 1’s by bringing 0’s before 1’s). Input Array: {0, 1, 1, 0, 0, 0, 0, 1, 1, 0} Output Array: {0, 0, 0, 0, 0, 0, 1, 1, 1, 1}

Shift elements in the array forward by k places

Given an array of m elements and an integer k ( total size of array >= m+k), . Write code to shift all the elements of array forward by k positions. For example: If k = 2 and the Input array is as given below, the the output array should be: The extra spaces will […]