# Plus One Leetcode Solution

## Plus One Leetcode Problem :

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.

## Plus One Leetcode Problem Solution :

### Constraints :

• 1 <= digits.length <= 100
• 0 <= digits[i] <= 9
• digits does not contain any leading 0’s.

### Example 1:

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

### Idea:

Simple Brute Force Approach.

### Approach:

Here, we are describing a process where we increment the last digit of an array outside of a loop, and then, within the loop, we have to check if any digit becomes 10.

If a digit becomes 10, you set it to 0 and increment the preceding digit by 1.

This process continues until there are no more 10s. Additionally, if the array has a single-digit number like 9, then add a 0 at the end and change it to 10.

Here’s the explanation for this process:

2. Increment the last digit by 1 outside of the loop.
3. Enter a loop to handle carrying over when a digit becomes 10.
4. Inside the loop, check if the current digit is 10.
5. If it’s 10, set it to 0 and increment the preceding digit by 1.
6. Continue this process until there are no more 10s.
7. If the first digit becomes 10, add a new leading 1 to the array.

Finally, you have your modified array.

### Complexity:

• Time Complexity:
Time complexity is O(n).

• Space Complexity:
Space complexity is O(1).

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others