Close

## Generating a pascal triangle

In this post, we will write a program to generate a pascal triangle of given height. For example, Pascal triangle of height 5 is shown below 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 The idea behind Pascal triangle is that each element in a row is the…

## Merging two sorted arrays

Given two sorted arrays A, B of lengths m, n respectively. How to merge them into a single sorted array?. Let us assume that A has sufficient space (m+n) for result array. For example let A = [1, 3, 5, 7, 9], and B = [2, 4, 6, 8, 10]. The function should return A…

## Removing all instances of a number in an array

Given an array of arbitrary numbers and a given value, how do we write a program to delete all instances of the given value? For example, if the input array is [23, 9, 18, 9, 6, 47, 3, 6] and the element to be deleted is 9. The result array should be [23, 18, 6,…

## Finding an element in a circularly sorted array

Given a circularly sorted array, how to search for an element efficiently? For example let us consider the array [12, 15, 6, 8, 10], the element 8 is found at index 3. Since the array is sorted but rotated, we can expect it to be solved using the binary search technique. We can design an…

## How many times a sorted array is rotated?

Given a circularly sorted array of unique elements, how do we efficiently find the number of rotations? For example the array [4, 5, 1, 2, 3] is (right) rotated twice. Before proceeding to find a solution, let’s note some observations. To find the number of rotations, we have to find the smallest element in the…