Safe Add (Number or BigInt)
JavaScript
Medium
4 views
Problem Description
Two integers are given as strings. If both fit safely in JavaScript Number and the sum is also safe, print the sum as Number. Otherwise print the sum using BigInt.
Input Format
One line: A B (integer strings).
Output Format
One line sum.
Sample Test Case
Input:
9007199254740991 1
Constraints
|A|,|B| up to 10^100000.
Official Solution
const fs=require('fs');const a=fs.readFileSync(0,'utf8').trim().split(/\\s+/);if(!a[0])process.exit(0);const A=a[0],B=a[1];const big=BigInt(A)+BigInt(B);const max=BigInt(Number.MAX_SAFE_INTEGER);const min=-max;let out;if(big<=max && big>=min){out=String(Number(big));}else{out=big.toString();}process.stdout.write(out);
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!