find range algorithm in java