Why start + (end – start)/2 is preferrable method for calculating middle of an array over (start + end)/2 ?

I am very sure that everyone is able to find middle index of array once you know start index and end index of array, but there are certain benefits of using start + (end – start)/2 over (start + end)/2, which are described below :

The very first way of finding middle index is

mid = (start + end)/2

But there is problem with this approach, what if value of start or end or both is INT_MAX, it will cause integer overflow.

The better way of calculating mid index is :

mid = start + (end - start)/2

Let’s try these both methods in C program :



filter_none

edit
close

play_arrow

link
brightness_4
code

// program for calculating mid of array
#include <stdio.h>
#include <limits.h>
int main()
{
    int start = INT_MAX, end = INT_MAX;
    printf("start = %dn", start);
    printf("end = %dn", end);
  
    // method 1
    int mid1 = (start + end) / 2;
    printf("mid using (start + end)/2 = %dn", mid1);
  
    // method 2
    int mid2 = start + (end - start) / 2;
    printf("mid using start + (end - start)/2 = %dn", mid2);
    return 0;
}

chevron_right


Output:


start = 2147483647
end = 2147483647
mid using (start + end)/2 = -1
mid using start + (end - start)/2 = 2147483647

Note : (end – start) may overflow if end < 0 or start < 0

If you see the output, by using second method you get correct output and first method fails to calculate mid and if you use this index (-1 in this case), it can cause segmentation fault because of invalid index of array.

start + (end – start)/2 also works even if you are using pointers :
Example :
Method 1

filter_none

edit
close

play_arrow

link
brightness_4
code

int s = 2, e = 3;
int* start = &s;
int* end = &e;
int* mid = (start + end) / 2;

chevron_right


Output :

error: invalid operands of types ‘int*’ and ‘int*’ to binary ‘operator+’
     int *mid = (start + end)/2;

Method 2

filter_none

edit
close

play_arrow

link
brightness_4
code

int s = 2, e = 3;
int* start = &s;
int* end = &e;
int* mid = start + (end - start) / 2;

chevron_right


Output :

It will compile and give expected results

Explanation : pointer addition is not supported in C while pointer subtraction is supported, the reason being the result of subtraction is the difference (in array elements) between the operands. The subtraction expression yields a signed integral result of type ptrdiff_t (defined in the standard include file STDDEF.H)(in short subtraction gives memory distance), but addition of two pointers in not meaningful, that’s why not supported

References :
1) Additive Operators: + and –
2) why-prefer-start-end-start-2-over-start-end-2-when-calculating-the
3) pointer-addition-vs-subtraction

This article is contributed by Mandeep Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up

Improved By : msdeep14

Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.