I guess my solution has passed all the test cases but failed on one.
Problem:
You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
Incrementing by one gives 123 1 = 124.
Thus, the result should be [1,2,4].
Example 2:
Input: digits = [9]
Output: [1,0]
Explanation: The array represents the integer 9.
Incrementing by one gives 9 1 = 10.
Thus, the result should be [1,0].
Constraints:
- 1 <= digits.length <= 100
- 0 <= digits[i] <= 9
- digits does not contain any leading 0's.
My solution:
var plusOne = function(digits) {
let arrToStr=digits.join('');
arrToStr ;
let strToArr = arrToStr.toString().split('').map((x)=>parseInt(x));
return strToArr;
};
Failed on this test cases:
Input:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,3]
Output:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,0,0,0]
Expected:
[6,1,4,5,3,9,0,1,9,5,1,8,6,7,0,5,5,4,4]
Is there anything I am doing wrong? Or Is it because of javascript? As I have read that javascript is not good for Competitive programming because it has some drawbacks.
CodePudding user response:
Integer in JavaScript can only represent up to 9,007,199,254,740,991 (https://stackoverflow.com/a/49218637/7588455)
6,145,390,195,186,705,543 is larger than that.
I recommend using BigInt
as an alternative.
A possible solution would look like this:
https://pastebin.com/NRHNYJT9 (Hidden so I don't spoiler you)