Adding one to number represented as array of digits
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 = 125Input : [9, 9, 9]
Output: 1000
Explanation: 999 + 1 = 1000
Table of Content
[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++ 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 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 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# 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 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());
}
Output
1000
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++ 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 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 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# 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 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());
}
Output
1000
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++ 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 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 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# 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 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());
}
Output
1000
Time Complexity: O(n), where n is the number of digits / size of the array.
Auxiliary Space: O(1)