What's the difference between
int x = (right + left) / 2;
and
int x = left + (right - left) / 2;
only I got a time limit exception in the first case and got in the second case when performing a binary search
The sum of your int variables
right + left (from the limit of integers)
is too large and exceeds the storage limit of integers, so there was an overflow due to the amount, but when you use the differential version, the second
left + (right - left) (within integers)
It is suitable for calculation and favors the machine.
The limit (border) int is 2,147,483,647.
Your right+left value does not match int .But the value of left + (right - left) / 2 less than int , so therefore the second expression works fine.
right+left
int
left + (right - left) / 2
if you add such large numbers, use long .
long
Source: https://habr.com/ru/post/1239224/More articles:create a mysql row with non-unique keys based on some other rows - sqlTry building a p2 site with mixed osgi and non osgi package - eclipseUncaught TypeError: Unable to read the "apply" property from undefined phaser - javascriptContent for gradle - eclipseUncaught TypeError: Unable to read the "apply" property from undefined with phaser - javascriptHow to capture a specific log file and show its contents in jenkins Console output - javaSitecore Date format using Sitecore (). Field ()? - c #Set the value of an argument in a class that inherits from int or float or str - pythonR scraper with jsessionid - rhttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1239229/adding-optional-parameters-to-the-constructors-of-multiply-inheriting-subclasses-of-built-in-types&usg=ALkJrhgEb2zMLXI0DhSJ8w_MMFrPli57pAAll Articles