Check for Integer Overflow (original) (raw)
Last Updated : 26 Mar, 2025
Given **two integers **a and **b. The task is to design a function that adds two integers and detects **overflow during the addition. If the sum does not cause an **overflow, return their **sum. Otherwise, return **-1 to indicate an **overflow.
**Note: You **cannot use type casting to a larger data type to check for overflow. Also, the function must handle both **positive and negative integers correctly.
**Examples:
**Input: a = 1000000000, b = 1000000000
**Output: 2000000000
**Explanation: The sum 1000000000 + 1000000000 = 2000000000 is within the valid integer range, so no overflow occurs, and the function stores the result successfully.**Input: a = -2000000000, b = -500000000
**Output: -1
**Explanation: The sum -2000000000 + (-5000000000) = -7000000000 exceeds the maximum limit of an integer (assuming 32 bit representation), causing an overflow.**Input: a = -100, b = 100
**Output: 0
**Explanation: The sum -100 + 100 = 0 is within the valid integer range, so no overflow occurs, and the function stores the result successfully.
Table of Content
- [Expected Approach] Observation on Signs of the Integers – O(1) Time and O(1) Space
- [Alternate Approach] Checking the Sum – O(1) Time and O(1) Space
**[Expected Approach] Observation on Signs of the Integers – O(1) Time and O(1) Space
The **idea is to detect overflow by observing how the sum behaves relative to the input values. The thought process is that **overflow can only occur if both numbers have the **same sign, but the sum has the **opposite sign. The **key observation is that in a typical integer representation, **exceeding the maximum or minimum limit causes a **wraparound effect, flipping the sign unexpectedly. By checking if this sign reversal happens, we can accurately determine **overflow and return an error indicator.
- Calculate sum
- If both numbers are positive and sum is negative then return -1
- OR If both numbers are negative and sum is positive then return -1
- Else return sum. C++ `
// C++ program to add two integers and detect overflow #include <bits/stdc++.h> using namespace std;
// Function to add two numbers and check for overflow int addOvf(int a, int b) {
// Calculate sum
int sum = a + b;
// Check for overflow conditions
if ((a > 0 && b > 0 && sum < 0)
|| (a < 0 && b < 0 && sum > 0)) {
return -1;
}
return sum;
}
int main() {
int a = 1000000000, b = 1000000000;
cout << addOvf(a, b) << endl;
return 0;
}
Java
// Java program to add two integers and detect overflow class GfG {
// Function to add two numbers and check for overflow
static int addOvf(int a, int b) {
// Calculate sum
int sum = a + b;
// Check for overflow conditions
if ((a > 0 && b > 0 && sum < 0)
|| (a < 0 && b < 0 && sum > 0)) {
return -1;
}
return sum;
}
public static void main(String[] args) {
int a = 1000000000, b = 1000000000;
System.out.println(addOvf(a, b));
}
}
Python
Python program to add two integers and detect overflow
Function to add two numbers and check for overflow
def addOvf(a, b):
# Calculate sum
sum = a + b
# Check for overflow conditions
if (a > 0 and b > 0 and sum < 0) or (a < 0 and b < 0 and sum > 0):
return -1
return sum
if name == "main":
a, b = 1000000000, 1000000000
print(addOvf(a, b))
C#
// C# program to add two integers and detect overflow using System;
class GfG {
// Function to add two numbers and check for overflow
static int addOvf(int a, int b) {
// Calculate sum
int sum = a + b;
// Check for overflow conditions
if ((a > 0 && b > 0 && sum < 0)
|| (a < 0 && b < 0 && sum > 0)) {
return -1;
}
return sum;
}
public static void Main() {
int a = 1000000000, b = 1000000000;
Console.WriteLine(addOvf(a, b));
}
}
JavaScript
// JavaScript program to add two integers and detect overflow
// Function to add two numbers and check for overflow function addOvf(a, b) {
// Calculate sum
let sum = a + b;
// Check for overflow conditions
if ((a > 0 && b > 0 && sum < 0)
|| (a < 0 && b < 0 && sum > 0)) {
return -1;
}
return sum;
}
let a = 1000000000, b = 1000000000;
console.log(addOvf(a, b));
`
**Time Complexity: O(1), due to constant number of operations (addition and comparisons).
**Space Complexity: O(1), as only a few integer variables are used.
[Alternate Approach] Checking the Sum – O(1) Time and O(1) Space
The **idea remains the same as in the previous approach, checking for integer overflow while performing addition. Instead of relying on **post-addition checks, we now **prevent overflow before it happens by ensuring that the sum does not exceed **Max Integer Value for positive numbers or go below **Min Integer Value for negative numbers.
C++ `
// C++ program to add two integers and detect overflow #include <bits/stdc++.h> using namespace std;
// Function to add two numbers and check // for overflow int addOvf(int a, int b) {
// Check for positive overflow
if (a >= 0 && b >= 0 && (a > INT_MAX - b)) {
return -1;
}
// Check for negative overflow
else if (a < 0 && b < 0 && (a < INT_MIN - b)) {
return -1;
}
// No overflow, compute sum
int result = a + b;
return result;
}
int main() {
int a = 1000000000, b = 1000000000;
cout << addOvf(a, b) << endl;
return 0;
}
Java
// Java program to add two integers and detect overflow class GfG {
// Function to add two numbers and check
// for overflow
static int addOvf(int a, int b) {
// Check for positive overflow
if (a >= 0 && b >= 0 && (a > Integer.MAX_VALUE - b)) {
return -1;
}
// Check for negative overflow
else if (a < 0 && b < 0 && (a < Integer.MIN_VALUE - b)) {
return -1;
}
// No overflow, compute sum
int result = a + b;
return result;
}
public static void main(String[] args) {
int a = 1000000000, b = 1000000000;
System.out.println(addOvf(a, b));
}
}
Python
Python program to add two integers and detect overflow
Function to add two numbers and check
for overflow
def addOvf(a, b):
# Check for positive overflow
if a >= 0 and b >= 0 and (a > (2**31 - 1) - b):
return -1
# Check for negative overflow
elif a < 0 and b < 0 and (a < (-2**31) - b):
return -1
# No overflow, compute sum
result = a + b
return result
if name == "main":
a, b = 1000000000, 1000000000
print(addOvf(a, b))
C#
// C# program to add two integers and detect overflow using System;
class GfG {
// Function to add two numbers and check
// for overflow
public static int addOvf(int a, int b) {
// Check for positive overflow
if (a >= 0 && b >= 0 && (a > int.MaxValue - b)) {
return -1;
}
// Check for negative overflow
else if (a < 0 && b < 0 && (a < int.MinValue - b)) {
return -1;
}
// No overflow, compute sum
int result = a + b;
return result;
}
public static void Main() {
int a = 1000000000, b = 1000000000;
Console.WriteLine(addOvf(a, b));
}
}
JavaScript
// JavaScript program to add two integers and detect overflow
// Function to add two numbers and check // for overflow function addOvf(a, b) {
// Check for positive overflow
if (a >= 0 && b >= 0 && (a > Number.MAX_SAFE_INTEGER - b)) {
return -1;
}
// Check for negative overflow
else if (a < 0 && b < 0 && (a < Number.MIN_SAFE_INTEGER - b)) {
return -1;
}
// No overflow, compute sum
let result = a + b;
return result;
}
// Driver Code let a = 1000000000, b = 1000000000;
console.log(addOvf(a, b));
`
**Time Complexity: O(1), due to constant number of operations (addition and comparisons).
**Space Complexity: O(1), as only a few integer variables are used.