solved using binary search