66. Plus One

Plus One (LeetCode 66) is a Easy Data Structures and Algorithms interview problem you can solve, run and submit on Thita.ai. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Plus One (Handling Carry) subpattern.

How to solve Plus One

Plus One is a Plus One problem, part of Array/Matrix Manipulation Patterns. Learn to handle carry operations when adding one to a number represented as an array of digits.

Work towards the solution in these steps:

  1. Consider how to represent and manipulate the number as an array of digits to perform arithmetic operations.
  2. Think about how you would add one to the number starting from the least significant digit and how it affects the digits.
  3. What happens if adding one to a digit causes it to become 10? How should you handle the carry to the next more significant digit?

The reference solution runs in O(n) time and O(1) space.

The Plus One solution and editorial walks through the full approach with reference code.

Problem statement

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 = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Incrementing by one gives 4321 + 1 = 4322. Thus, the result should be [4,3,2,2].

Example 3:

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.

Topics and companies

Topics: Array, Math.

Reported in interviews at ByteDance, Facebook, Adobe, Amazon, Spotify, Google.

How to practise Plus One on Thita.ai

Starter code is provided in C, C#, C++, Go, Java, JavaScript and Python. Your solution runs against 5 test cases for this problem, with the AI coach available for a hint when you are stuck rather than a finished answer. The reference solution runs in O(n) time and O(1) space.

Open Plus One in the code editor, or read the Plus One editorial for a worked solution with its approach and complexity analysis.

Where Plus One sits in the DSA pattern sheet

Problems related to Plus One

Other problems that use the same Plus One (Handling Carry) and Array/Matrix Manipulation Patterns techniques:

Plus One FAQ

What is the LeetCode number of Plus One?

Plus One is LeetCode problem 66, rated Easy.

What pattern is used to solve Plus One?

Plus One is a Plus One problem. It belongs to the Array/Matrix Manipulation Patterns pattern, in the Plus One (Handling Carry) subpattern.

What is the time complexity of Plus One?

The reference solution runs in O(n) time and O(1) space.

Which companies ask Plus One?

Plus One has been reported in interviews at ByteDance, Facebook, Adobe, Amazon, Spotify, Google.

Preparing this workspace.