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 = 455Input: arr[] = {6}, K = 815
Output: 821
6 + 815 = 821
Approach: Start traversing the digits array from the end and start adding the digits of K one by one to the current digit, if any carry is generated then add it to the next addition of digits. After all the digits of K have been added, start traversing the digits array from the left and start printing the elements which on concatenation will give the resultant number.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the vector containing the answer vector<int> addToArrayForm(vector<int>& A, int K) { // Vector v is to store each digits sum // and vector ans is to store the answer vector<int> v, ans; // No carry in the beginning int rem = 0; int i = 0; // Start loop from the end // and take element one by one for (i = A.size() - 1; i >= 0; i--) { // Array index and last digit of number int my = A[i] + K % 10 + rem; if (my > 9) { // Maintain carry of summation rem = 1; // Push the digit value into the array v.push_back(my % 10); } else { v.push_back(my); rem = 0; } K = K / 10; } // K value is greater then 0 while (K > 0) { // Push digits of K one by one in the array int my = K % 10 + rem; v.push_back(my % 10); // Also maintain carry with summation if (my / 10 > 0) rem = 1; else rem = 0; K = K / 10; } if (rem > 0) v.push_back(rem); // Reverse the elements of vector v // and store it in vector ans for (int i = v.size() - 1; i >= 0; i--) ans.push_back(v[i]); return ans; } // Driver code int main() { vector<int> A{ 2, 7, 4 }; int K = 181; vector<int> ans = addToArrayForm(A, K); // Print the answer for (int i = 0; i < ans.size(); i++) cout << ans[i]; return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the vector containing the answer static ArrayList<Integer> addToArrayForm(ArrayList<Integer> A, int K) { // ArrayList v is to store each digits sum // and ArrayList ans is to store the answer ArrayList<Integer> v = new ArrayList<Integer>(); ArrayList<Integer> ans = new ArrayList<Integer>(); // No carry in the beginning int rem = 0; int i = 0; // Start loop from the end // and take element one by one for (i = A.size() - 1; i >= 0; i--) { // Array index and last digit of number int my = A.get(i) + K % 10 + rem; if (my > 9) { // Maintain carry of summation rem = 1; // Push the digit value into the array v.add(my % 10); } else { v.add(my); rem = 0; } K = K / 10; } // K value is greater then 0 while (K > 0) { // Push digits of K one by one in the array int my = K % 10 + rem; v.add(my % 10); // Also maintain carry with summation if (my / 10 > 0) rem = 1; else rem = 0; K = K / 10; } if (rem > 0) v.add(rem); // Reverse the elements of vector v // and store it in vector ans for (int j = v.size() - 1; j >= 0; j--) ans.add(v.get(j)); return ans; } // Driver code public static void main (String[] args) { ArrayList<Integer> A = new ArrayList<Integer>(); A.add(2); A.add(7); A.add(4); int K = 181; ArrayList<Integer> ans = addToArrayForm(A, K); // Print the answer for (int i = 0; i < ans.size(); i++) System.out.print(ans.get(i)); } } // This code is contributed by ihritik |
Python3
# Python3 implementation of the approach # Function to return the vector # containing the answer def addToArrayForm(A, K): # Vector v is to store each digits sum # and vector ans is to store the answer v, ans = [], [] # No carry in the beginning rem, i = 0, 0 # Start loop from the end # and take element one by one for i in range(len(A) - 1, -1, -1): # Array index and last digit of number my = A[i] + (K % 10) + rem if my > 9: # Maintain carry of summation rem = 1 # Push the digit value into the array v.append(my % 10) else: v.append(my) rem = 0 K = K // 10 # K value is greater then 0 while K > 0: # Push digits of K one by one in the array my = (K % 10) + rem v.append(my % 10) # Also maintain carry with summation if my // 10 > 0: rem = 1 else: rem = 0 K = K // 10 if rem > 0: v.append(rem) # Reverse the elements of vector v # and store it in vector ans for i in range(len(v) - 1, -1, -1): ans.append(v[i]) return ans # Driver code if __name__ == "__main__": A = [2, 7, 4] K = 181 ans = addToArrayForm(A, K) # Print the answer for i in range(0, len(ans)): print(ans[i], end = "") # This code is contributed by Rituraj Jain |
C#
// C# implementation of the approach using System; using System.Collections; class GFG { // Function to return the vector containing the answer static ArrayList addToArrayForm(ArrayList A, int K) { // ArrayList v is to store each digits sum // and ArrayList ans is to store the answer ArrayList v = new ArrayList(); ArrayList ans = new ArrayList(); // No carry in the beginning int rem = 0; int i = 0; // Start loop from the end // and take element one by one for (i = A.Count - 1; i >= 0; i--) { // Array index and last digit of number int my = (int)A[i] + K % 10 + rem; if (my > 9) { // Maintain carry of summation rem = 1; // Push the digit value into the array v.Add(my % 10); } else { v.Add(my); rem = 0; } K = K / 10; } // K value is greater then 0 while (K > 0) { // Push digits of K one by one in the array int my = K % 10 + rem; v.Add(my % 10); // Also maintain carry with summation if (my / 10 > 0) rem = 1; else rem = 0; K = K / 10; } if (rem > 0) v.Add(rem); // Reverse the elements of vector v // and store it in vector ans for (int j = v.Count - 1; j >= 0; j--) ans.Add((int)v[j]); return ans; } // Driver code static void Main() { ArrayList A = new ArrayList(); A.Add(2); A.Add(7); A.Add(4); int K = 181; ArrayList ans = addToArrayForm(A, K); // Print the answer for (int i = 0; i < ans.Count; i++) Console.Write((int)ans[i]); } } // This code is contributed by mits |
PHP
<?php // Php implementation of the approach // Function to return the vector containing the answer function addToArrayForm($A, $K) { // Vector v is to store each digits sum // and vector ans is to store the answer $v = array(); $ans = array(); // No carry in the beginning $rem = 0; $i = 0; // Start loop from the end // and take element one by one for ($i = count($A) - 1; $i >= 0; $i--) { // Array index and last digit of number $my = $A[$i] + $K % 10 + $rem; if ($my > 9) { // Maintain carry of summation $rem = 1; // Push the digit value into the array array_push($v,$my % 10); } else { array_push($v,$my); $rem = 0; } $K = floor($K / 10); } // K value is greater then 0 while ($K > 0) { // Push digits of K one by one in the array $my = $K % 10 + $rem; array_push($v,$my % 10); // Also maintain carry with summation if ($my / 10 > 0) $rem = 1; else $rem = 0; $K = floor($K / 10); } if ($rem > 0) array_push($v,$rem); // Reverse the elements of vector v // and store it in vector ans for ($i = count($v) - 1; $i >= 0; $i--) array_push($ans,$v[$i]); return $ans; } // Driver code $A = array( 2, 7, 4 ); $K = 181; $ans = addToArrayForm($A, $K); // Print the answer for ($i = 0; $i < count($ans); $i++) echo $ans[$i]; // This code is contributed by Ryuga ?> |
455
Recommended Posts:
- Check if a given number can be represented in given a no. of digits in any base
- Find the smallest positive number which can not be represented by given digits
- Count the number of digits of palindrome numbers in an array
- Check if given number can be represented as sum of two great numbers
- Check if a number can be represented as a sum of 2 triangular numbers
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Add 1 to number represented as array | Recursive Approach
- Check whether a large number represented as array is divisible by Y
- Check in binary array the number represented by a subarray is odd or even
- Minimum sum of two numbers formed from digits of an array
- Minimum sum of two numbers formed from digits of an array in O(n)
- Find last k digits in product of an array numbers
- Minimum sum of two numbers formed from digits of an array
- Check if two numbers have same number of digits
- Number of digits in the product of two numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



