LeetCode Problem - Link

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].



Sequential Solution [No fancy smarty stuff]

I was able to whip out a solution very quickly this way, but one of the test cases had a really big int which a primitive integer of JavaScript cannot handle. So utilizing BigInt is necessary to solve this in JavaScript. I had also learned how to perform operations on a BigIn (it's not like how we do usually)


/**
 * @param {number[]} digits
 * @return {number[]}
 */
var plusOne = function (digits) {
    let num = '';
    for (var i = 0; i < digits.length; i++) {
        num += digits[i]
    }
    let num_plus_one = BigInt(num) // need this to handle really big numbers in JS
    num_plus_one += 1n // adding 1 to BigInt ends with n

    num_plus_one += '' // typecast to string
    var str_arr = num_plus_one.split('')


    var new_arr = []
    for (var i = 0; i < str_arr.length; i++) {
        new_arr.push(parseInt(str_arr[i]))
    }
    return new_arr
};