# 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

### How to prepare for your next Coding Interview

Here are some tips while preparing for your coding interviews. 1. Do study or…

### Index for Coding Problems

Sorting Problems Merge Sort Quick Sort Heap Sort Bubble Sort Selection Sort…

### Counting Inversions Coding Problem

** Inversion There is an array(a) and two indexes i and j. Inversion is the…

### Young Tableau problem - Cormen

Young Tableau A a X b matrix is Young Tableau if all rows(from left to right…

### 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…

### Binary Search Tree (BST) Data Structure

A Binary Search tree (BST) is a data structure which has two children nodes…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…