Introduction
This is a request from one of the readers. This is mostly useful in competitive programming where you want to sort an array by the values, but at the same time the requirement is to preserve the index of each element.
If I have to explain using an example, then consider the array below. The array is zero indexed.
The first two rows represent the input array and indices of each element in the array. The next two rows represent the sorted array, but the moment you sort it, the index for each element changes. Earlier A was at index 4 but in the sorted array it is at index ...

Read More
# arrays

## Find the Point of Rotation in Sorted Array

Problem Statement
You are given a sorted array. However, there is a problem. Someone just rotated the sorted array by K spaces and we do not know the value of K.
Write a program to find the value of K by which the array is rotated. As the array has millions of numbers, it would be good to have a solution which takes minimal time.
Approach to Find the Point of Rotation
Brute Force
Let us try out my favorite approach, the Brute Force Method. It is quite simple, below are the steps:
Start traversing the array from the beginning
There will be a index in the array where the value sto...

Read More
## Find the maximum possible profit

Problem Statement
Given an array of stock prices per share for a given share over a time period. Calculate the maximum possible profit per share one could have made . Here we do not consider short selling as an option. The trader must have purchased the stocks before selling it.
The problem statement is a real world definition of the problem called maximum positive difference.
For the above two arrays, here are the maximum profit per share one could have made:
A : 18.0 (difference between 21 and 3)
B : 13.0 (difference between 19 and 6)
What is Maximum Positive Difference?
...

Read More
## Count Triangles formed by the elements of array

Problem Statement
Given a sorted array of distinct integers, each of these integers can represent a length. The task at hand is to count the number of possible triangles which can be made through these lengths.
Of course, if you chose one length to be one side of the triangle, you cannot use that length again in the same triangle. This means, no triangle contains the same length more than once.
Important Note: For given sides of lengths a, b and c. A triangle can only be formed if sum of any two sides is greater than the third side. This is called the triangle inequality. The sum of any...

Read More
## Minimum Range over K arrays

Introduction
This is one interesting problem with a non-trivial solution, this has been asked in couple of good interviews. Here goes the problem statement.
"Given K positive integer arrays, each of which contains elements in sorted order. Find a range of integers such that it must contain at least one element from each array and the range is minimum."
May be a diagram can make this more clear. Here you go!
Understanding the Minimum Range over K arrays problem.
In the above diagram we have 3 arrays each containing elements in increasing order. Now, let us identify a range which cont...

Read More