Detect overflow on add
Java
Medium
5 views
Problem Description
Task: return true if a+b overflows 32-bit int.
Output Format
Return value
Constraints
Use long check.
Official Solution
static boolean addOverflows(int a,int b){long s=(long)a+(long)b;return s>Integer.MAX_VALUE||s<Integer.MIN_VALUE;}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!