Adding one to number represented as array of digits
Last Updated :
13 Feb, 2025
Given a non-negative number represented as an array of digits. The is to add 1 to the number (increment the number represented by the digits by 1). The digits are stored such that the most significant digit is the first element of the array.
Examples :
Input : [1, 2, 4]
Output : 125
Explanation: 124 + 1 = 125
Input : [9, 9, 9]
Output: 1000
Explanation: 999 + 1 = 1000
[Approach - 1] - Using Carry - O(n) Time and O(1) Space
To add one to the number represented by digits, follow the below steps :
- Parse the given array from the end as we do in school addition.
- If the last elements are 9, make it 0 and carry = 1.
- For the next iteration check carry and if it adds to 10, do the same as step 2.
- After adding carry, make carry = 0 for the next iteration.
- If the carry still remains after traversing the entire array, append 1 in the beginning.
C++
// C++ program to add 1 to a
// number represented as an array
#include <bits/stdc++.h>
using namespace std;
vector<int> addOne(vector<int> &arr) {
int carry = 1;
for(int i = arr.size() - 1; i >= 0; i--) {
int sum = arr[i] + carry;
arr[i] = sum % 10;
carry = sum / 10;
}
if(carry) {
arr.insert(arr.begin(), carry);
}
return arr;
}
int main() {
vector<int> arr = {9, 9, 9};
vector<int> res = addOne(arr);
for(auto i:res) {
cout << i;
}
return 0;
}
Java
// Java program to add 1 to a
// number represented as an array
import java.util.*;
class GFG {
static int[] addOne(int[] arr) {
int carry = 1;
for(int i = arr.length - 1; i >= 0; i--) {
int sum = arr[i] + carry;
arr[i] = sum % 10;
carry = sum / 10;
}
if(carry > 0) {
int[] newArr = new int[arr.length + 1];
newArr[0] = carry;
System.arraycopy(arr, 0, newArr, 1, arr.length);
return newArr;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
for(int i : res) {
System.out.print(i);
}
}
}
Python
# Python program to add 1 to a
# number represented as an array
def addOne(arr):
carry = 1
for i in range(len(arr) - 1, -1, -1):
sum = arr[i] + carry
arr[i] = sum % 10
carry = sum // 10
if carry:
arr.insert(0, carry)
return arr
# Driver code
if __name__ == "__main__":
arr = [9, 9, 9]
res = addOne(arr)
for i in res:
print(i, end="")
C#
// C# program to add 1 to a
// number represented as an array
using System;
using System.Collections.Generic;
class GFG {
static int[] addOne(int[] arr) {
int carry = 1;
for(int i = arr.Length - 1; i >= 0; i--) {
int sum = arr[i] + carry;
arr[i] = sum % 10;
carry = sum / 10;
}
if(carry > 0) {
int[] newArr = new int[arr.Length + 1];
newArr[0] = carry;
Array.Copy(arr, 0, newArr, 1, arr.Length);
return newArr;
}
return arr;
}
public static void Main() {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
foreach(int i in res) {
Console.Write(i);
}
}
}
JavaScript
// JavaScript program to add 1 to a
// number represented as an array
function addOne(arr) {
let carry = 1;
for(let i = arr.length - 1; i >= 0; i--) {
let sum = arr[i] + carry;
arr[i] = sum % 10;
carry = Math.floor(sum / 10);
}
if(carry > 0) {
arr.unshift(carry);
}
return arr;
}
// Driver code
let arr = [9, 9, 9];
let res = addOne(arr);
for(let i of res) {
process.stdout.write(i.toString());
}
Time Complexity: O(n), where n is the size of array.
Space Complexity: O(1)
[Approach - 2] - O(n) Time and O(1) Space
The idea is to start from the end of the vector and if the element is 9 set it to 0, else increment the digit by 1 and exit the loop.
- If the loop set all digits to 0 (if all digits were 9) insert 1 at the beginning.
- Else increment the element at the position where the loop stopped.
C++
// C++ program to add 1 to a
// number represented as an array
#include <bits/stdc++.h>
using namespace std;
// function to add one
vector<int> addOne(vector<int> &arr) {
// initialize an index to end of array
int index = arr.size() - 1;
// while the index is valid and the value
// at index is 9
while (index >= 0 && arr[index] == 9)
arr[index--] = 0;
// if index < 0 (if all arr were 9)
if (index < 0)
// insert an one at the beginning of the vector
arr.insert(arr.begin(), 1, 1);
// else increment the value at [index]
else
arr[index]++;
return arr;
}
int main() {
vector<int> arr = {9, 9, 9};
vector<int> res = addOne(arr);
for(auto i:res) {
cout << i;
}
return 0;
}
Java
// Java program to add 1 to a
// number represented as an array
import java.util.*;
class GFG {
// function to add one
static int[] addOne(int[] arr) {
// initialize an index to end of array
int index = arr.length - 1;
// while the index is valid and the value
// at index is 9
while (index >= 0 && arr[index] == 9)
arr[index--] = 0;
// if index < 0 (if all arr were 9)
if (index < 0) {
// insert an one at the beginning of the array
int[] newArr = new int[arr.length + 1];
newArr[0] = 1;
System.arraycopy(arr, 0, newArr, 1, arr.length);
return newArr;
}
// else increment the value at [index]
else
arr[index]++;
return arr;
}
public static void main(String[] args) {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
for (int i : res) {
System.out.print(i);
}
}
}
Python
# Python program to add 1 to a
# number represented as an array
# function to add one
def addOne(arr):
# initialize an index to end of array
index = len(arr) - 1
# while the index is valid and the value
# at index is 9
while index >= 0 and arr[index] == 9:
arr[index] = 0
index -= 1
# if index < 0 (if all arr were 9)
if index < 0:
# insert an one at the beginning of the list
arr.insert(0, 1)
# else increment the value at [index]
else:
arr[index] += 1
return arr
if __name__ == "__main__":
# Driver code
arr = [9, 9, 9]
res = addOne(arr)
for i in res:
print(i, end="")
C#
// C# program to add 1 to a
// number represented as an array
using System;
using System.Collections.Generic;
class GFG {
// function to add one
static int[] addOne(int[] arr) {
// initialize an index to end of array
int index = arr.Length - 1;
// while the index is valid and the value
// at index is 9
while (index >= 0 && arr[index] == 9)
arr[index--] = 0;
// if index < 0 (if all arr were 9)
if (index < 0) {
// insert an one at the beginning of the array
int[] newArr = new int[arr.Length + 1];
newArr[0] = 1;
Array.Copy(arr, 0, newArr, 1, arr.Length);
return newArr;
}
// else increment the value at [index]
else
arr[index]++;
return arr;
}
public static void Main() {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
foreach (int i in res) {
Console.Write(i);
}
}
}
JavaScript
// JavaScript program to add 1 to a
// number represented as an array
// function to add one
function addOne(arr) {
// initialize an index to end of array
let index = arr.length - 1;
// while the index is valid and the value
// at index is 9
while (index >= 0 && arr[index] === 9)
arr[index--] = 0;
// if index < 0 (if all arr were 9)
if (index < 0) {
// insert an one at the beginning of the array
arr.unshift(1);
}
// else increment the value at [index]
else
arr[index]++;
return arr;
}
// Driver code
let arr = [9, 9, 9];
let res = addOne(arr);
for (let i of res) {
process.stdout.write(i.toString());
}
Time Complexity: O(n), where n is the number of digits / size of the array.
Auxiliary Space: O(1)
[Alternate Approach] - O(n) Time and O(1) Space
To avoid inserting at front of array, we can firstly reverse the array, and then append the value at the last. Thereafter, we can reverse the array again to get the result.
C++
// C++ program to add 1 to a
// number represented as an array
#include <bits/stdc++.h>
using namespace std;
// function to add one
vector<int> addOne(vector<int> &arr) {
// reverse the digits
reverse(arr.begin(), arr.end());
// initialize an index to start of array
int index = 0;
// while the index is valid and the value
// at index is 9
while (index < arr.size() && arr[index] == 9)
arr[index++] = 0;
// if index == arr.size() (if all arr were 9)
if (index == arr.size())
// insert an one at the end
arr.push_back(1);
// else increment the value at [index]
else
arr[index]++;
// reverse the array
reverse(arr.begin(), arr.end());
return arr;
}
int main() {
vector<int> arr = {9, 9, 9};
vector<int> res = addOne(arr);
for(auto i:res) {
cout << i;
}
return 0;
}
Java
// Java program to add 1 to a
// number represented as an array
import java.util.*;
class GFG {
// function to add one
static int[] addOne(int[] arr) {
// reverse the digits
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// initialize an index to start of array
int index = 0;
// while the index is valid and the value
// at index is 9
while (index < arr.length && arr[index] == 9)
arr[index++] = 0;
// if index == arr.length (if all arr were 9)
if (index == arr.length) {
// insert an one at the end
int[] newArr = new int[arr.length + 1];
System.arraycopy(arr, 0, newArr, 0, arr.length);
newArr[arr.length] = 1;
arr = newArr;
}
// else increment the value at [index]
else
arr[index]++;
// reverse the array
for (int i = 0, j = arr.length - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return arr;
}
public static void main(String[] args) {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
for (int i : res) {
System.out.print(i);
}
}
}
Python
# Python program to add 1 to a
# number represented as an array
# function to add one
def addOne(arr):
# reverse the digits
arr.reverse()
# initialize an index to start of array
index = 0
# while the index is valid and the value
# at index is 9
while index < len(arr) and arr[index] == 9:
arr[index] = 0
index += 1
# if index == len(arr) (if all arr were 9)
if index == len(arr):
# insert an one at the end
arr.append(1)
# else increment the value at [index]
else:
arr[index] += 1
# reverse the array
arr.reverse()
return arr
# Driver code
if __name__ == "__main__":
arr = [9, 9, 9]
res = addOne(arr)
for i in res:
print(i, end="")
C#
// C# program to add 1 to a
// number represented as an array
using System;
using System.Collections.Generic;
class GFG {
// function to add one
static int[] addOne(int[] arr) {
// reverse the digits
Array.Reverse(arr);
// initialize an index to start of array
int index = 0;
// while the index is valid and the value
// at index is 9
while (index < arr.Length && arr[index] == 9)
arr[index++] = 0;
// if index == arr.Length (if all arr were 9)
if (index == arr.Length) {
// insert an one at the end
int[] newArr = new int[arr.Length + 1];
Array.Copy(arr, newArr, arr.Length);
newArr[arr.Length] = 1;
arr = newArr;
}
// else increment the value at [index]
else
arr[index]++;
// reverse the array
Array.Reverse(arr);
return arr;
}
public static void Main() {
int[] arr = {9, 9, 9};
int[] res = addOne(arr);
foreach (int i in res) {
Console.Write(i);
}
}
}
JavaScript
// JavaScript program to add 1 to a
// number represented as an array
// function to add one
function addOne(arr) {
// reverse the digits
arr.reverse();
// initialize an index to start of array
let index = 0;
// while the index is valid and the value
// at index is 9
while (index < arr.length && arr[index] === 9)
arr[index++] = 0;
// if index == arr.length (if all arr were 9)
if (index === arr.length) {
// insert an one at the end
arr.push(1);
}
// else increment the value at [index]
else
arr[index]++;
// reverse the array
arr.reverse();
return arr;
}
// Driver code
let arr = [9, 9, 9];
let res = addOne(arr);
for (let i of res) {
process.stdout.write(i.toString());
}
Time Complexity: O(n), where n is the number of digits / size of the array.
Auxiliary Space: O(1)
Similar Reads
Sum of two numbers where one number is represented as array of digits Given an array arr[] of digits and an integer K, the task is to find num(arr) + K where num(arr) is the number formed by concatenating all the digits of the array. Examples: Input: arr[] = {2, 7, 4}, K = 181 Output: 455 274 + 181 = 455 Input: arr[] = {6}, K = 815 Output: 821 6 + 815 = 821 Approach:
10 min read
Add two numbers represented by two arrays Given two array A[0....n-1] and B[0....m-1] of size n and m respectively, representing two numbers such that every element of arrays represent a digit. For example, A[] = { 1, 2, 3} and B[] = { 2, 1, 4 } represent 123 and 214 respectively. The task is to find the sum of both the number. In above cas
15+ min read
Add 1 to number represented as array | Recursive Approach Given an array which represents a number, add 1 to the array. Suppose an array contain elements [1, 2, 3, 4] then the array represents decimal number 1234 and hence adding 1 to this would result 1235. So new array will be [1, 2, 3, 5]. Examples: Input : [1, 2, 3, 4] Output : [1, 2, 3, 5] Input : [1,
6 min read
Javascript Program For Adding 1 To A Number Represented As Linked List Number is represented in linked list such that each digit corresponds to a node in linked list. Add 1 to it. For example 1999 is represented as (1-> 9-> 9 -> 9) and adding 1 to it should change it to (2->0->0->0) Below are the steps : Reverse given linked list. For example, 1->
5 min read
Sum of an array of large numbers Given an integer K and an array arr[] consisting of N large numbers in the form of strings, the task is to find the sum of all the large numbers of the array. Examples: Input: K = 50, arr[] = {â01234567890123456789012345678901234567890123456789â, â01234567890123456789012345678901234567890123456789â,
10 min read