Advertisement

Plus One - LeetCode 66 Solution

Plus One - Complete Solution Guide

Plus One is LeetCode problem 66, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given a large integer represented as an integer array digits , where each digits[i] is the i th 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

Detailed Explanation

This problem asks us to increment a 'large integer' by one. The interesting twist is that this integer isn't given as a standard `int` or `long`, but as an array of its individual digits. For instance, `[1,2,3]` represents the number 123. The digits are ordered from most significant to least significant, left-to-right, and there are no leading zeros, except for the number 0 itself (which would be `[0]`). The core challenge here lies in accurately simulating elementary addition, specifically handling 'carries'. When you add one to a number like 123, it's straightforward: 123 + 1 = 124, so `[1,2,3]` becomes `[1,2,4]`. However, consider `[4,5,9]`. Adding one makes it 460. This means the `9` becomes `0`, and a `1` is carried over to the `5`, making it `6`. This carry-propagation is where the logic needs to be robust. The most intriguing edge case arises when the number consists entirely of nines, like `[9,9]`. Adding one gives us 100. This means `[9,9]` must transform into `[1,0,0]`. Not only do all the nines become zeros due to carry propagation, but the array itself needs to grow in length to accommodate the new most significant digit. This highlights why a simple `int` conversion isn't suitable, as `[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9]` would quickly overflow standard integer types.

Solution Approach

The provided solution elegantly simulates the manual process of adding one to a number, starting from the rightmost (least significant) digit and moving left. It iterates backward through the `digits` array. The key insight is that if a digit is less than 9, incrementing it by one will never cause a carry to propagate further left. For example, if we have `[1,2,3]` and are at `3`, we increment it to `4`. The result `[1,2,4]` is final, and we can immediately return. This covers the vast majority of cases and allows for an early exit. However, if the current digit `digits[i]` *is* 9, we must set it to 0. This simulates a carry being generated and propagated to the left. The loop then continues to the next digit to the left, effectively 'carrying the one' to it. If the loop completes, it means every digit in the original array was a 9 (e.g., `[9,9]`). In this specific scenario, all original digits would have been turned into zeros (`[0,0]`). We then need to prepend a `1` to this array to form the final result (`[1,0,0]`), representing the number with an increased digit count.

Step-by-Step Algorithm

  1. Step 1: Iterate through the `digits` array from right to left (from the least significant digit to the most significant digit).
  2. Step 2: If the current digit is less than 9, increment it by 1, and return the modified `digits` array.
  3. Step 3: If the current digit is 9, set it to 0 and continue to the next digit to the left (handle carry-over).
  4. Step 4: If the loop completes (all digits were 9), create a new array with size one greater than the original array. Set the first element of the new array to 1, and copy the remaining elements (all 0s) from the original array.
  5. Step 5: Return the new array.

Key Insights

  • **Right-to-Left Carry Propagation:** The natural way to add to a number is from the least significant digit. The solution correctly models this by iterating the `digits` array from `n-1` down to `0`, ensuring carries are handled sequentially.
  • **Early Exit Optimization:** If a digit `digits[i]` is found to be less than `9`, we can simply increment it, and crucially, `return the array immediately`. No further carries will propagate left, making this a highly efficient shortcut for most inputs.
  • **Elegant Handling of All Nines Edge Case:** The solution cleanly manages the scenario where all digits are 9s (e.g., `[9,9,9]`). If the loop finishes, it implies all digits were converted to 0s. The solution then prepends a `1` to this all-zero array (`[1] + digits`), effectively creating `[1,0,0,0]` without complex array resizing logic, which is both readable and efficient.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: Array, Math.

Companies

Asked at: Accenture, Adobe, Agoda, Amazon, Apple, Bloomberg, Intuit, Meta, Microsoft, TikTok, Uber, Visa, Yahoo, tcs.