# Leetcode Plus One problem solution

Apr 17, 2023

In the Leetcode Plus One problem solution 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.

## Solution in C Programming

``````/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* plusOne(int* digits, int n, int* m){
int count=0;
for(int i=0;i<n;i++){
if(digits[i]==9)
count++;
}
if(count==n){
*m = n+1;
int *ptr= (int *)malloc((n+1)*sizeof(int));
ptr[0]=1;
for(int i=1;i<n+1;i++){
ptr[i]=0;
}
return ptr;
}
else{
*m = n;
int *ptr= (int *)malloc((n)*sizeof(int));
for(int i=0;i<n;i++){
ptr[i]=digits[i];
}
if(ptr[n-1]==9){
for(int i=n-1;i>=0;i--){
if(ptr[i]==9){
ptr[i]=0;
}
else{
ptr[i]=ptr[i]+1;
break;
}
}
}
else{
ptr[n-1]=ptr[n-1]+1;
}
return ptr;
}
}``````

## Solution in C++ Programming

``````class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
if(digits.back()!=9)
{
digits.back()++;
return digits;
}
int ptr=digits.size()-1;
while(ptr>=0 && digits[ptr]==9)
{

digits[ptr]=0;
ptr--;

}

if(ptr==-1)
{
digits.insert(digits.begin(),1);
return digits;
}

digits[ptr]++;

return digits;

}
};``````

## Solution in Java Programming

``````class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
int carrier = 1;
for (int i = n - 1; i >= 0; i--) {
int tmp = digits[i] + carrier;
digits[i] = tmp % 10;
carrier = tmp / 10;
}
if (carrier == 0)
return digits;
int[] result = new int[n + 1];
result[0] = carrier;
System.arraycopy(digits, 0, result, 1, n);
return result;
}
}``````

## Solution in Python Programming

``````class Solution(object):
def plusOne(self, digits):
"""
:type digits: List[int]
:rtype: List[int]
"""
forward = 1
for i in range(len(digits))[::-1]:
forward, digits[i] = (digits[i] + forward) / 10, (digits[i] + forward) % 10
if forward == 0:
return digits
return [forward] + digits``````

## Solution in C# Programming

``````public class Solution {
public int[] PlusOne(int[] digits) {
for (int i = digits.Length - 1; i >= 0; i--) {
int digit = digits[i];

// If num is less than 9 simply increment
if (digit < 9) {
digits[i] = digit + 1;
return digits;
}
else {
// Set current num to 0 and there will be a carry.
digits[i] = 0;
}
}

// Edge case when all values are 9's
int[] newDigitArray = new int [digits.Length + 1];
newDigitArray[0] = 1;

return newDigitArray;
}
}``````

#### By Neha Singhal

Hi, my name is Neha singhal a software engineer and coder by profession. I like to solve coding problems that give me the power to write posts for this site.