# Plus One - Leet Code Solution

September 03, 2020

## Problem Statement

Given a non-empty array of digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example

``````Input: digits = [1,2,3]
Output: [1,2,4]``````

## Solution

It has very simple solution, just by iterating array from the end. And, keeping track of carry.
A simple complexity here is that result can be bigger than original array if we got a carry for the last sum.

### Code

``````public int[] plusOne(int[] digits) {
int l = digits.length;

//initializing carry with the number we want to add for first time.
int carry = 1;

for (int i=l-1; i>=0; i--) {
digits[i] = digits[i] + carry;

carry = digits[i]/10;
digits[i] = digits[i]%10;
}

// copy result to another array
int targetSize = carry == 1 ? l+1 : l;
int[] res = new int[targetSize];

int i=0;
if (carry == 1) {
res[0] = carry;
i = 1;
}
for (; i<targetSize; i++) {
res[i] = digits[i-carry];
}
return res;
}``````

### Complexity

Its `O(n)`

## Similar Posts

### Convert String to Integer - atoi - Leet Code Solution

Problem Statement Implement atoi which converts a string to an integer…

### How to calculate First Common Ancestor of two Nodes in Binary Tree

First try to understand question. Its a binary tree, not a binary search tree…

System design interview is pretty common these days, specially if you are having…

In this post, we will see some of the frequently used concepts/vocabulary in…

### Validate Sudoku - Leet Code Solution

Problem Statement Determine if a 9x9 Sudoku board is valid. Only the filled…

### Counting Sort Algorithm

Counting sort runs on relatively smaller set of input. Counting sort calculates…

## Latest Posts

### Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

### Python SMTP Email Code - Sender Address Rejected - Not Owned By User

Introduction In a normal email sending code from python, I’m getting following…

### Nodejs with MongoDB - Number of Opened Connections Keep on Increasing with Mongoose Library

Introduction In one of my app, I was using to talk to . I have used some event…

### Django Python - How to Build Docker Image and Run Web-service on Apache with Python 3.9

Introduction So you have a Django project, and want to run it using docker image…

### Python - How to Maintain Quality Build Process Using Pylint and Unittest Coverage With Minimum Threshold Values

Introduction It is very important to introduce few process so that your code and…

### Example Jenkin Groovy Pipeline Script for Building Python Projects with Git Events and Push to Artifactory

Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…