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
C++
#include <bits/stdc++.h> using namespace std; /* 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; } // Driver code int main() { int *res = new int[(sizeof(int))]; int x = 2147483640; int y = 10; cout<<addOvf(res, x, y); cout<<"\n"<<*res; return 0; } // This code is contributed by rathbhupendra |
C
#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; } |
-1 -2147483646
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.
C++
#include <bits/stdc++.h> using namespace std; 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 = new int[(sizeof(int))]; int x = 2147483640; int y = 10; cout<<addOvf(res, x, y)<<endl; cout<<*res; return 0; } // This code is contributed by rathbhupendra |
C
#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; } |
-1 0
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Cyclic shifts of integer N by another integer m
- Multiply a given Integer with 3.5
- How to swap two bits in a given integer?
- Find One's Complement of an Integer
- Count set bits in an integer
- Find closest integer with the same weight
- Next greater integer having one more number of set bits
- Compute the integer absolute value (abs) without branching
- Minimum numbers needed to express every integer below N as a sum
- Count set bits in an integer using Lookup Table
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
- Previous smaller integer having one less number of set bits
- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized
- Find the largest number smaller than integer N with maximum number of set bits
- Check whether K-th bit is set or not
- Check whether a given number is even or odd
- Efficiently check whether n is a multiple of 4 or not
- Check whether the number has only first and last bits set
- Check whether all the bits are set in the given range
- Check if a number is power of 8 or not
Improved By : rathbhupendra

