Skip to content
Related Articles

Related Articles

Adding one to number represented as array of digits
  • Difficulty Level : Medium
  • Last Updated : 06 Aug, 2019

Given a non-negative number represented as an array of digits, add 1 to the number ( increment the number represented by the digits ). The digits are stored such that the most significant digit is first element of array.

Examples :

Input : [1, 2, 4]
Output : [1, 2, 5]

Input : [9, 9, 9]
Output : [1, 0, 0, 0]

Approach : To add one to number represented by digits, follow the below steps :

  • Parse the given array from end like we do in school addition.
  • If the last elements 9, make it 0 and carry = 1.
  • For the next iteration check carry and if it adds to 10, do same as step 2.
  • After adding carry, make carry = 0 for the next iteration.
  • If the vectors add and increase the vector size, append 1 in the beginning.

Below is the implementation to add 1 to number represented by digits.

C++




// CPP implementation for Adding one
// to number represented by digits
#include <bits/stdc++.h>
using namespace std;
  
// function for adding one to number
void incrementVector(vector<int> &a)
{
    int n = a.size();
  
    // Add 1 to last digit and find carry
    a[n-1] += 1;
    int carry = a[n-1]/10;
    a[n-1] = a[n-1] % 10;
  
    // Traverse from second last digit
    for (int i=n-2; i>=0; i--)
    {
        if (carry == 1)
        {
           a[i] += 1;
           carry = a[i]/10;
           a[i] = a[i] % 10;
        }
    }
  
    // If carry is 1, we need to add
    // a 1 at the beginning of vector
    if (carry == 1)
      a.insert(a.begin(), 1);
}
  
// driver code
int main()
{
    vector<int> vect{1, 7, 8, 9};
  
    incrementVector(vect);
  
    for (int i=0; i<vect.size(); i++)
       cout << vect[i] << " ";
  
    return 0;
}

Java




// Java implementation for Adding one
// to number represented by digits
import java.io.*;
import java.util.*;
  
class GFG {
      
    // function for adding one to number
    static void incrementVector(Vector<Integer> a)
    {
        int n = a.size();
      
        // Add 1 to last digit and find carry
        a.set(n - 1, a.get(n - 1) + 1);
        int carry = a.get(n - 1) / 10;
        a.set(n - 1, a.get(n - 1) % 10);
      
        // Traverse from second last digit
        for (int i = n - 2; i >= 0; i--)
        {
            if (carry == 1)
            {
            a.set(i, a.get(i) + 1);
            carry = a.get(i) / 10;
            a.set(i, a.get(i) % 10);
            }
        }
      
        // If carry is 1, we need to add
        // a 1 at the beginning of vector
        if (carry == 1)
        a.add(0, 1);
    }
      
    // Driver code
    public static void main (String[] args) 
    {
        Vector<Integer> vect=new Vector<Integer>();
        vect.add(1);
        vect.add(7);
        vect.add(8);
        vect.add(9);
          
        incrementVector(vect);
      
        for (int i = 0; i < vect.size(); i++)
        System.out.print(vect.get(i) + " ");
        }
    }
  
// This code is contributed by Gitanjali.

C#




// C# implementation for Adding one
// to number represented by digits
using System;
using System.Xml;
  
namespace myTry
{
    class Program
    {
        // Driver code
        static void Main(string[] args)
        {         
            int carry = 0;
            int[] array = new int[] {1,7,8,9};
  
            // find the length of the array
            int n = array.Length;
  
            // Add 1 to the last digit and find carry 
            array[n - 1] += 1;
            carry = array[n - 1] / 10;
            array[n - 1] = array[n - 1] % 10;
  
            // Traverse from second last digit 
            for(int i = n - 2; i >= 0; i--)
            {
                if(carry == 1)
                {
                    array[i] += 1;
                    carry = array[i] / 10;
                    array[i] = array[i] % 10;
                }
            }
  
            // If the carry is 1, we need to add
            // a 1 at the beginning of the array
            if(carry == 1)
            {
                Array.Resize(ref array, n+1);
                array[0] = carry;
            }
  
            for (int i = 0; i < array.Length; i++)
                Console.WriteLine(array[i] + " ");
              
        }
    }
}

Python3




# Python implementation for Adding one
# to number represented by digits
  
import math
  
# function for adding one to number
def incrementVector(a):
  
    n = len(a)
   
    # Add 1 to last digit and find carry
    a[n-1] += 1
    carry = a[n-1]/10
    a[n-1] = a[n-1] % 10
   
    # Traverse from second last digit
    for i in range(n-2,-1,-1):
        if (carry == 1):
           a[i] += 1
           carry = a[i]/10
           a[i] = a[i] % 10
          
    # If carry is 1, we need to add
    # a 1 at the beginning of vector
    if (carry == 1):
      a.insert(0, 1)
  
# driver code
vect=[1, 7, 8, 9]
  
incrementVector(vect)
  
for  i in range(0, len(vect)):
     print(vect[i] ,end= " ")
  
# This code is contributed by Gitanjali.


Output:
1 7 9 0

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up
Recommended Articles
Page :