I don't know anything about ForkJoin
, but this looks suspicious just on general principles:
if(lastElem - firstElem == 2){
if(array[firstElem] > array[lastElem])
return array[firstElem];
else
return array[lastElem];
}
The rest of your code makes it appear that you're processing a subarray from firstElem
(inclusive) to lastElem
(exclusive), so shouldn't you be using lastElem-1
to index?