
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding one to number represented as array of digits in C++?
In this problem, we are given a number in the form of an array, and each digit of the number is stored at one index of the array, and the most significant digit comes first. Our task is to add 1 to this number and return the updated number in the same array format.
Let's understand it with some example scenarios.
Scenario 1
Input: Input_arr[] = {2, 6, 1} Output: 262 Explanation: The array represents 261. Adding 1 gives 261 + 1 = 262.
Scenario 2
Input: input_arr[] = {5, 9, 9, 9} Output: 6000 Explanation: The array represents 5999. Adding 1 gives 5999 + 1 = 6000. Each 9 becomes 0 with a carry to the left, and finally the 5 increments to 6, so the result becomes 6000.
To solve this problem in C++, we'll use the following three methods:
In all methods, we'll use a vector instead of a regular array because vectors are dynamic. They can grow in size automatically, which helps when we need to insert a new digit during the addition.
Carry Handling from the End
To handle carry from the end, we start from the last digit and move toward the front while maintaining a carry. We add 1 to the last digit, and if it becomes 10, we set that digit to 0 and carry 1 to the left. This continues until no carry is left. If a carry remains after the loop (like when all digits are 9), we insert 1 at the front of the vector.
Example
The following is the complete C++ program. Here, we add 1 to the given number and return the result:
#include <iostream> #include <vector> using namespace std; // Function to add 1 to the number represented as a vector of digits vector<int> addOneToNumber(vector<int> &numberDigits) { int carry = 1; for (int i = numberDigits.size() - 1; i >= 0; i--) { int sum = numberDigits[i] + carry; numberDigits[i] = sum % 10; carry = sum / 10; } if (carry) { numberDigits.insert(numberDigits.begin(), carry); } return numberDigits; } // Function to print a vector of digits void printNumber(const vector<int>& number) { for (int digit : number) { cout << digit << " "; } cout << endl; } int main() { vector<int> inputNumber = {9, 9, 9}; // Input number as digits cout << "Input number: "; printNumber(inputNumber); vector<int> result = addOneToNumber(inputNumber); cout << "Output number after adding 1: "; printNumber(result); return 0; }
Below is the output of the above program, where we display both the input and the result after adding one to it.
Input number: 9 9 9 Output number after adding 1: 1 0 0 0
Time Complexity: O(n) because we go through each digit once from right to left.
Space Complexity: O(1) because we update the input vector directly.
Set End Digits to Zero and Increment
In this method, we start from the last digit and move left. If a digit is 9, we set it to 0. When we find a digit that's not 9, we increase it by 1 and stop. If all digits are 9 (like [9, 9, 9]), we set all to 0 and insert 1 at the beginning to handle the carry.
Example
Following is a complete C++ program where we add 1 to the given array of digits. The program updates the input array and then prints the final result.
#include <iostream> #include <vector> using namespace std; // Function to add 1 to a number represented as digits in a vector vector<int> incrementNumberByOne(vector<int>& digitList) { int position = digitList.size() - 1; // Set trailing 9s to 0 until we find a digit to increment while (position >= 0 && digitList[position] == 9) { digitList[position] = 0; position--; } // If all digits were 9, insert 1 at the beginning if (position < 0) { digitList.insert(digitList.begin(), 1); } else { digitList[position]++; } return digitList; } // Function to print the number void printDigits(const vector<int>& digits) { for (int digit : digits) { cout << digit << " "; } cout << endl; } int main() { vector<int> inputNumber = {9, 9, 9}; // You can test with other inputs too cout << "Input number: "; printDigits(inputNumber); vector<int> updatedNumber = incrementNumberByOne(inputNumber); cout << "Output number after adding 1: "; printDigits(updatedNumber); return 0; }
Following is the output of the above program, where we display both the input number and the output after adding 1 to it.
Input number: 9 9 9 Output number after adding 1: 1 0 0 0
Time Complexity: O(n) because we may go through all digits in the worst case (like in all 9s).
Space Complexity: O(1) because we modify the input vector directly without using extra space.
Reverse First, Then Process
To add 1 to the given number, we first reverse the array. Then, we start processing from the beginning of the reversed array. If a digit is 9, we set it to 0 and carry 1 to the next digit. If a digit is not 9, we simply increase it by 1 and stop. Finally, we reverse the array again and return the result.
Example
Here is a complete C++ program where we add 1 to the given array of digits and print the updated number.
#include <iostream> #include <vector> #include <algorithm> // for reverse using namespace std; // Function to add 1 to a number represented by a vector of digits vector<int> addOneUsingReverse(vector<int>& numberDigits) { reverse(numberDigits.begin(), numberDigits.end()); // reverse the digits int index = 0; while (index < numberDigits.size() && numberDigits[index] == 9) { numberDigits[index] = 0; index++; } // Check if all digits were 9 i.e, index reached end of array if (index == numberDigits.size()) { numberDigits.push_back(1); } else { numberDigits[index]++; } reverse(numberDigits.begin(), numberDigits.end()); // reverse back to original order return numberDigits; } // Function to print digits nicely void printNumber(const vector<int>& number) { for (int digit : number) { cout << digit << " "; } cout << endl; } int main() { vector<int> inputNumber = {9, 9, 9}; // You can change this input for testing cout << "Input number: "; printNumber(inputNumber); vector<int> result = addOneUsingReverse(inputNumber); cout << "Output number after adding 1: "; printNumber(result); return 0; }
The output of the above program is given below:
Input number: 9 9 9 Output number after adding 1: 1 0 0 0
Time Complexity: O(n) because we reverse the vector twice and visit each digit once.
Space Complexity: O(1) because we modify the input vector without using extra space.
Conclusion
In this article, we learned how to add 1 to a number represented as an array of digits. We covered three simple C++ methods to solve this problem using vectors. Each method handles the carry in a different way but gives the correct result.