Finding Middle Element in an Array

Shouldn't it be ' middle = maxIndex/2 '. No, because this might not work if the starting index is not 1 or 0 or in other words, this will not work for subarrays. 

Firstly, let's see how to find the number of elements in the given array.  Click here for whiteboard.  I found finding the number of elements in an array or subarray is bit difficult if we get to know only the indices. Each programmer can pass the indices in a different way. 

 

The last formula satisfies for all the index numbering, so formula to find the number of elements in an array is

middle = (high -low + 1)/2

So, now the middle index would be 3 if there are 6 elements. Now, it is the programmer's responsibility to get the proper subarray with the correct index value. 

So again this isn't the proper formula for finding middle as programmer is responsible to pass the proper subindex. In a long way, this may lead to other issues. 

So, let's jumble the formula and try to find the best. 

                       middle = (high + low - 1)/2


Comments