Given a sorted array which contains duplicate elements, we have to write a java code to find first and last position of a number in a sorted array. Given a number x, write a code to find it’s first and last index in an array.

For example –

Suppose the input array is arr[] : **{1, 4, 7, 8, 8, 11, 11, 11, 11, 12, 13, 13}** and number to searched is **11** then the first index of 11 is 5 and last index is 8.