Write a “C” function, int addOvf(int* result, int a, int b) If there is no overflow, the function places the resultant = sum a+b in “result” and returns 0. Otherwise it returns -1. The solution of casting to long and adding to find detecting the overflow is not allowed.

**Method 1**

There can be overflow only if signs of two numbers are same, and sign of sum is opposite to the signs of numbers.

1) Calculate sum 2) If both numbers are positive and sum is negative then return -1 Else If both numbers are negative and sum is positive then return -1 Else return 0

`#include<stdio.h> ` `#include<stdlib.h> ` ` ` `/* Takes pointer to result and two numbers as ` ` ` `arguments. If there is no overflow, the function ` ` ` `places the resultant = sum a+b in “result” and ` ` ` `returns 0, otherwise it returns -1 */` ` ` `int` `addOvf(` `int` `* result, ` `int` `a, ` `int` `b) ` ` ` `{ ` ` ` `*result = a + b; ` ` ` `if` `(a > 0 && b > 0 && *result < 0) ` ` ` `return` `-1; ` ` ` `if` `(a < 0 && b < 0 && *result > 0) ` ` ` `return` `-1; ` ` ` `return` `0; ` ` ` `} ` ` ` ` ` `int` `main() ` ` ` `{ ` ` ` `int` `*res = (` `int` `*)` `malloc` `(` `sizeof` `(` `int` `)); ` ` ` `int` `x = 2147483640; ` ` ` `int` `y = 10; ` ` ` ` ` `printf` `(` `"%d"` `, addOvf(res, x, y)); ` ` ` ` ` `printf` `(` `"\n %d"` `, *res); ` ` ` `getchar` `(); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity : O(1)

Space Complexity: O(1)

**Method 2**

Thanks to Himanshu Aggarwal for adding this method. This method doesn’t modify *result if there us an overflow.

`#include<stdio.h> ` `#include<limits.h> ` `#include<stdlib.h> ` ` ` `int` `addOvf(` `int` `* result, ` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `( a > INT_MAX - b) ` ` ` `return` `-1; ` ` ` `else` ` ` `{ ` ` ` `*result = a + b; ` ` ` `return` `0; ` ` ` `} ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `*res = (` `int` `*)` `malloc` `(` `sizeof` `(` `int` `)); ` ` ` `int` `x = 2147483640; ` ` ` `int` `y = 10; ` ` ` ` ` `printf` `(` `"%d"` `, addOvf(res, x, y)); ` ` ` `printf` `(` `"\n %d"` `, *res); ` ` ` `getchar` `(); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

Time Complexity : O(1)

Space Complexity: O(1)

Please write comments if you find any bug in above codes/algorithms, or find other ways to solve the same problem

## Recommended Posts:

- Write an Efficient Method to Check if a Number is Multiple of 3
- Count set bits in an integer
- Compute the integer absolute value (abs) without branching
- Multiply a given Integer with 3.5
- Check if a number is multiple of 9 using bitwise operators
- Check if binary representation of a number is palindrome
- Check if a given number is sparse or not
- Cyclic Redundancy Check and Modulo-2 Division
- Check if a number is Bleak
- Check whether a given number is even or odd
- Check if a number can be expressed as a sum of consecutive numbers
- Check if bits of a number has count of consecutive set bits in increasing order
- Check if a number has bits in alternate pattern | Set 1
- Check divisibility in a binary stream
- Efficiently check if a string has duplicates without using any additional data structure