Plus One

medium

By - Aman Pareek

Last Updated - 05/09/2024

Problem Statement

You are given a large integer as an array of digits, where each element in the array represents a single digit of the integer. The digits are arranged from the most significant to the least significant. Importantly, there are no leading zeros in this array.

Your goal is to increment this large integer by one and return the updated integer in the same array format.

Write a function that efficiently handles the incrementation of large integers represented as arrays and returns the result as an updated array of digits. Ensure that your solution adheres to the constraints and correctly handles edge cases.

Example 1

Input: array = [1,2,3]

Output: [1,2,4]

Example 2

Input: array = [4,3,2,1]

Output: [4,3,2,2]

Example 3

Input: array = [9]

Output: [1,0]

Constraints

  • The length of digits will be between 1 and 100.

  • Each digit in the array will be between 0 and 9.

  • The array will not have any leading zeros.

Solution 1: Basic Approach with Carry Handling

function plusOneBasic(digits) {
    for (let i = digits.length - 1; i >= 0; i--) {
        if (digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        digits[i] = 0;
    }
    return [1, ...digits];
} 

const array1 = [1,2,3];
plusOneBasic(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneBasic(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneBasic(array3);  //output: [1,0] 

Solution 2: Using Array.prototype.reduceRight()

function plusOneReduceRight(digits) {
    return digits.reduceRight((acc, num, index) => {
        if (index === digits.length - 1) {
            num++;
        }
        if (num > 9) {
            acc[index] = 0;
            acc.unshift(1);
        } else {
            acc[index] = num;
        }
        return acc;
    }, Array(digits.length).fill(0));
} 

const array1 = [1,2,3];
plusOneReduceRight(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneReduceRight(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneReduceRight(array3);  //output: [1,0] 

Solution 3: Using BigInt

function plusOneBigInt(digits) {
    const num = BigInt(digits.join('')) + BigInt(1);
    return num.toString().split('').map(Number);
} 

const array1 = [1,2,3];
plusOneBigInt(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneBigInt(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneBigInt(array3);  //output: [1,0] 

Solution 4: Using a Custom Helper Function for Carry

function plusOneCustomCarry(digits) {
    let carry = 1;
    for (let i = digits.length - 1; i >= 0; i--) {
        let sum = digits[i] + carry;
        digits[i] = sum % 10;
        carry = Math.floor(sum / 10);
    }
    if (carry > 0) {
        digits.unshift(carry);
    }
    return digits;
} 

const array1 = [1,2,3];
plusOneCustomCarry(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneCustomCarry(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneCustomCarry(array3);  //output: [1,0] 

Solution 5: Using String Manipulation

function plusOneStringManipulation(digits) {
    const numStr = digits.join('');
    const incrementedNum = (BigInt(numStr) + BigInt(1)).toString();
    return incrementedNum.split('').map(Number);
} 

const array1 = [1,2,3];
plusOneStringManipulation(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneStringManipulation(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneStringManipulation(array3);  //output: [1,0] 

Solution 6: Handling All Nines

function plusOneAllNines(digits) {
    if (digits.every(digit => digit === 9)) {
        return [1, ...Array(digits.length).fill(0)];
    }
    
    for (let i = digits.length - 1; i >= 0; i--) {
        if (digits[i] < 9) {
            digits[i]++;
            return digits;
        }
        digits[i] = 0;
    }
    return digits;
} 

const array1 = [1,2,3];
plusOneAllNines(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneAllNines(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneAllNines(array3);  //output: [1,0] 

Solution 7: Using Array.prototype.concat()

function plusOneConcat(digits) {
    let carry = 1;
    const result = digits.slice().reverse().map(num => {
        let sum = num + carry;
        carry = Math.floor(sum / 10);
        return sum % 10;
    }).reverse();

    if (carry > 0) {
        result.unshift(carry);
    }
    return result;
} 

const array1 = [1,2,3];
plusOneConcat(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneConcat(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneConcat(array3);  //output: [1,0] 

Solution 8: Using Array.prototype.reduce() with Concatenation

function plusOneReduceConcat(digits) {
    let carry = 1;
    const result = digits.reduceRight((acc, num) => {
        let sum = num + carry;
        carry = Math.floor(sum / 10);
        acc.unshift(sum % 10);
        return acc;
    }, []);

    if (carry > 0) {
        result.unshift(carry);
    }
    return result;
} 

const array1 = [1,2,3];
plusOneReduceConcat(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneReduceConcat(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneReduceConcat(array3);  //output: [1,0] 

Solution 9: Using Decomposition

function plusOneDecomposition(digits) {
    let carry = 1;
    const result = [];
    
    for (let i = digits.length - 1; i >= 0; i--) {
        let sum = digits[i] + carry;
        carry = Math.floor(sum / 10);
        result.push(sum % 10);
    }
    
    if (carry > 0) {
        result.push(carry);
    }
    
    return result.reverse();
} 

const array1 = [1,2,3];
plusOneDecomposition(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneDecomposition(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneDecomposition(array3);  //output: [1,0] 

Solution 10: Using Array.prototype.splice()

function plusOneSplice(digits) {
    let carry = 1;
    let i = digits.length - 1;
    
    while (i >= 0 && carry > 0) {
        let sum = digits[i] + carry;
        digits[i] = sum % 10;
        carry = Math.floor(sum / 10);
        i--;
    }
    
    if (carry > 0) {
        digits.splice(0, 0, carry);
    }
    
    return digits;
} 

const array1 = [1,2,3];
plusOneSplice(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneSplice(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneSplice(array3);  //output: [1,0] 

Solution 11: Using Mathematical Computations

function plusOneMath(digits) {
    let number = digits.reduce((acc, digit) => acc * 10 + digit, 0);
    number++;
    return number.toString().split('').map(Number);
} 

const array1 = [1,2,3];
plusOneMath(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneMath(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneMath(array3);  //output: [1,0] 

Solution 12: Using Recursion

function plusOneRecursive(digits) {
    function helper(digits, index) {
        if (index < 0) {
            return [1, ...digits];
        }
        
        if (digits[index] < 9) {
            digits[index]++;
            return digits;
        }
        
        digits[index] = 0;
        return helper(digits, index - 1);
    }
    
    return helper(digits, digits.length - 1);
} 

const array1 = [1,2,3];
plusOneRecursive(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneRecursive(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneRecursive(array3);  //output: [1,0] 

Solution 13: Using Array.prototype.forEach()

function plusOneForEach(digits) {
    let carry = 1;
    digits.reverse().forEach((num, index) => {
        let sum = num + carry;
        digits[index] = sum % 10;
        carry = Math.floor(sum / 10);
    });
    digits.reverse();
    
    if (carry > 0) {
        digits.unshift(carry);
    }
    
    return digits;
} 

const array1 = [1,2,3];
plusOneForEach(array1);  //output: [1,2,4] 

const array2 = [4,3,2,1];
plusOneForEach(array2);  //output: [4,3,2,2] 

const array3 = [9];
plusOneForEach(array3);  //output: [1,0]