you can sort the array with merge sort and then get the index directly