Thursday, December 26, 2024
Google search engine
HomeLanguagesJavaCheck for Integer Overflow

Check for Integer Overflow

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;
}


Java




// Java program for the above approach
class GFG
{
 
/* 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 */
static 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
public static void main(String args[]) {
    int res = -2147483646; // size of an Integer
    int x = 2147483640;
    int y = 10;
 
    System.out.println(addOvf(res, x, y));
    System.out.print(res);
}
}
 
// This code is contributed by sanjoy_62.


C#




// C# program to of the above approach
using System;
class GFG {
 
/* 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 */
static 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
public static void Main()
{
    int res = -2147483646; // size of an Integer
    int x = 2147483640;
    int y = 10;
 
    Console.WriteLine(addOvf(res, x, y));
    Console.Write(res);
}
}
 
// This code is contributed by code_hunt.


Javascript




<script>
        // JavaScript code for the above approach
 
/* 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 */
function addOvf(result, a, b)
{
    let resultt = a + b;
    if(a > 0 && b > 0 && result < 0)
        return -1;
    if(a < 0 && b < 0 && result > 0)
        return -1;
    return 0;
}
 
    // Driver Code
 
    let res = -2147483646; // size of an Integer
    let x = 2147483640;
    let y = 11;
 
    document.write(addOvf(res, x, y) + "<br/>");
    document.write(res);
        
       // This code is contributed by sanjoy_62.
    </script>


Python3




# 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
def addOvf(result, a, b):
    resultt = a + b;
    if(a > 0 and b > 0 and result < 0):
        return -1;
    if(a < 0 and b < 0 and result > 0):
        return -1;
    return 0;
 
# Driver Code
res = -2147483646 # size of an Integer
x = 2147483640
y = 11
 
print(addOvf(res, x, y))
print(res)
    
 
# This code is contributed by phasing17.


Output: 

-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 >= 0 && b >= 0 && (a > INT_MAX - b)) {
        return -1;
    }
 
    else if (a < 0 && b < 0 && (a < INT_MIN - b)) {
        return -1;
    }
    else {
        *result = a + b;
        return 0;
    }
}
 
int main()
{
    int a, b;
    int* res;
    a = INT_MAX;
    b = 8192;
 
    cout << addOvf(res, a, b);
 
    return 0;
}
 
// This code is contributed by Sonu Kumar Pandit


C




#include <limits.h>
#include <stdio.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;
}


Java




// Java code to implement the approach
class GFG {
 
  public static int addOvf(int[] result, int a, int b)
  {
    // Check if a + b will result in overflow
    if (a >= 0 && b >= 0
        && (a > Integer.MAX_VALUE - b)) {
      return -1;
    }
    else if (a < 0 && b < 0
             && (a < Integer.MIN_VALUE - b)) {
      return -1;
    }
    else {
      // Store the result in the first element of the
      // result array
      result[0] = a + b;
      return 0;
    }
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int a, b;
    int[] res = new int[1];
    a = Integer.MAX_VALUE;
    b = 8192;
 
    // Function call
    System.out.println(addOvf(res, a, b));
  }
}
 
// This code is contributed by phasing17


Python3




import sys
 
def addOvf(result, a, b):
    # Check if the sum of a and b is greater than INT_MAX
    # or if a and b are both negative and their sum is less than INT_MIN
    if (a >= 0 and b >= 0 and (a > sys.maxsize - b)) or (a < 0 and b < 0 and (a < -sys.maxsize - b)):
        return -1
    else:
        result[0] = a + b
        return 0
 
if __name__ == "__main__":
    a = sys.maxsize
    b = 8192
    res = [0]
    print(addOvf(res, a, b))


C#




// C# code to implement the approach
using System;
class GFG {
 
  public static int addOvf(int[] result, int a, int b)
  {
    // Check if a + b will result in overflow
    if (a >= 0 && b >= 0 && (a > int.MaxValue - b)) {
      return -1;
    }
    else if (a < 0 && b < 0 && (a < int.MinValue - b)) {
      return -1;
    }
    else {
      // Store the result in the first element of the
      // result array
      result[0] = a + b;
      return 0;
    }
  }
 
  // Driver code
  public static void Main(string[] args)
  {
    int a, b;
    int[] res = new int[1];
    a = int.MaxValue;
    b = 8192;
 
    // Function call
    Console.WriteLine(addOvf(res, a, b));
  }
}
 
// This code is contributed by phasing17


Javascript




function addOvf(result, a, b) {
  if (a >= 0 && b >= 0 && a > Number.MAX_SAFE_INTEGER - b) {
    return -1;
  } else if (a < 0 && b < 0 && a < Number.MIN_SAFE_INTEGER - b) {
    return -1;
  } else {
    result[0] = a + b;
    return 0;
  }
}
 
let a = 0, b = 0;
let res = [0];
a = Number.MAX_SAFE_INTEGER;
b = 8192;
 
console.log(addOvf(res, a, b));
 
//This code is contributed by rudra1807raj


Output

-1

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
 

RELATED ARTICLES

Most Popular

Recent Comments