Sum of integer value of input Array and reverse Array
Given an integer array of numbers num[], the task is to write a function to return the sum of the value of the array with its reverse value and then return the resulting array of digits.
Examples:
Input: num[] = [2, 5, 8, 2]
Output: [5, 4, 3, 4]
Explanation: Given array [2, 5, 8, 2], the numerical value is 2582 and the reverse value is 2852, so the sum is 2582 + 2852 = [5, 4, 3, 4]Input: num[] = [2, 4, 6]
Output: [8, 8, 8]
Explanation: Given array [2, 4, 6] the numerical value is 246 and the reverse value is 642, so the sum is 246 + 642 = [8, 8, 8]
Approach: The approach for solving this problem is as follows:
- Convert the input integer array num[] into a single integer value num by concatenating all the elements of the array as strings and then converting the result into an integer.
- Reverse the input array number to get a new array reverse_number.
- Convert the reversed array reverse_number into a single integer value reverse_num using the same method as described in step 1.
- Find the sum of num and reverse_num and store it in a variable sum_num.
- Convert the value of sum_num into a list of individual digits and return it as the result.
Below is the implementation for the above approach:
C++
// C++ code for the above approach: #include <bits/stdc++.h> std::vector< int > sum_of_array_and_its_reverse( const std::vector< int >& number) { int num = 0, mul = 1; for ( int i = number.size() - 1; i >= 0; i--) { num += number[i] * mul; mul *= 10; } int rev_num = 0, mul_rev = 1; for ( int i = 0; i < number.size(); i++) { rev_num += number[i] * mul_rev; mul_rev *= 10; } int result = num + rev_num; std::vector< int > res; while (result > 0) { res.push_back(result % 10); result /= 10; } std::reverse(res.begin(), res.end()); return res; } // Driver code int main() { std::vector< int > number1 = { 2, 5, 8, 2 }; std::vector< int > result1 = sum_of_array_and_its_reverse(number1); for ( int i : result1) { std::cout << i << " " ; } std::cout << std::endl; std::vector< int > number2 = { 5, 5, 5 }; std::vector< int > result2 = sum_of_array_and_its_reverse(number2); for ( int i : result2) { std::cout << i << " " ; } std::cout << std::endl; return 0; } |
Java
import java.util.ArrayList; import java.util.Collections; import java.util.List; class Main { static List<Integer> sum_of_array_and_its_reverse(List<Integer> number) { int num = 0 , mul = 1 ; for ( int i = number.size() - 1 ; i >= 0 ; i--) { num += number.get(i) * mul; mul *= 10 ; } int rev_num = 0 , mul_rev = 1 ; for ( int i = 0 ; i < number.size(); i++) { rev_num += number.get(i) * mul_rev; mul_rev *= 10 ; } int result = num + rev_num; List<Integer> res = new ArrayList<>(); while (result > 0 ) { res.add(result % 10 ); result /= 10 ; } Collections.reverse(res); return res; } public static void main(String[] args) { List<Integer> number1 = new ArrayList<>(); number1.add( 2 ); number1.add( 5 ); number1.add( 8 ); number1.add( 2 ); List<Integer> result1 = sum_of_array_and_its_reverse(number1); System.out.println(result1); List<Integer> number2 = new ArrayList<>(); number2.add( 2 ); number2.add( 4 ); number2.add( 6 ); List<Integer> result2 = sum_of_array_and_its_reverse(number2); System.out.println(result2); } } |
Python3
def sum_of_array_and_its_reverse(number): num = int ("".join( str (x) for x in number)) rev_num = int ("".join( str (x) for x in reversed (number))) result = num + rev_num return [ int (x) for x in str (result)] print (sum_of_array_and_its_reverse([ 2 , 5 , 8 , 2 ])) # Output: [5, 4, 3, 4] print (sum_of_array_and_its_reverse([ 2 , 4 , 6 ])) # Output: [8, 8, 8] |
Javascript
function sum_of_array_and_its_reverse(number) { let num = 0, mul = 1; for (let i = number.length - 1; i >= 0; i--) { num += number[i] * mul; mul *= 10; } let rev_num = 0, mul_rev = 1; for (let i = 0; i < number.length; i++) { rev_num += number[i] * mul_rev; mul_rev *= 10; } let result = num + rev_num; let res = []; while (result > 0) { res.push(result % 10); result = Math.floor(result / 10); } res.reverse(); return res; } let number1 = [2, 5, 8, 2]; let result1 = sum_of_array_and_its_reverse(number1); console.log(result1); let number2 = [2, 4, 6]; let result2 = sum_of_array_and_its_reverse(number2); console.log(result2); |
C#
using System; using System.Linq; namespace SumOfArrayWithReverse { class Program { static void Main( string [] args) { int [] number = new int [] { 9, 9, 9 }; int [] result = SumArrayWithReverse(number); Console.WriteLine( "Result: " + string .Join( ", " , result)); int [] number2 = new int [] { 5, 5, 5 }; int [] result2 = SumArrayWithReverse(number2); Console.WriteLine( "\nResult: " + string .Join( ", " , result2)); } static int [] SumArrayWithReverse( int [] number) { int [] reverseNumber = number.Reverse().ToArray(); int [] result = new int [number.Length]; int carry = 0; for ( int i = 0; i < number.Length; i++) { int sum = number[i] + reverseNumber[i] + carry; result[i] = sum % 10; carry = sum / 10; } if (carry > 0) { Array.Resize( ref result, result.Length + 1); result[result.Length - 1] = carry; } return result.Reverse().ToArray(); } } } |
5 4 3 4 1 1 1 0
Time complexity: O(n) where n is the length of the input array
This is because the code iterates through the input array twice, once to compute the original number and once to compute the reversed number. The size of the result vector is also proportional to the size of the input array, so iterating through the result vector has a time complexity of O(n) as well. Therefore, the overall time complexity of the function is O(n), which is linear with respect to the size of the input.
Auxiliary Space: O(n) where n is the size of the input vector.
This is because the code creates a new vector to store the digits of the result, which can also be of size n in the worst case.
Method: Numerical reverse-and-add approach
The numerical reverse-and-add approach involves the following steps:
- Convert the array to a numerical value: Iterate through the array and multiply each digit by its corresponding place value to obtain the numerical value of the array.
- Reverse the array: Reverse the array to obtain the reversed array.
- Convert the reversed array to a numerical value: Iterate through the reversed array and multiply each digit by its corresponding place value to obtain the numerical value of the reversed array.
- Add the two numerical values: Add the numerical values obtained in steps 1 and 3 to obtain the sum.
- Convert the sum to an array of digits: Repeatedly divide the sum by 10 and take the remainder to obtain each digit. Add each digit to the resulting array until the sum becomes 0. Reverse the resulting array to obtain the final array of digits.
This approach can be used to efficiently compute the sum of the value of an array with its reverse value.
Python3
def reverse_sum(num): # Convert the array to a numerical value n = len (num) num_value = 0 for i in range (n): num_value + = num[i] * 10 * * (n - i - 1 ) # Reverse the array and obtain the numerical value of the reversed array reverse_num = num[:: - 1 ] reverse_value = 0 for i in range (n): reverse_value + = reverse_num[i] * 10 * * (n - i - 1 ) # Add the two numerical values sum_value = num_value + reverse_value # Convert the sum to an array of digits result = [] while sum_value > 0 : result.append(sum_value % 10 ) sum_value / / = 10 return result[:: - 1 ] print (reverse_sum([ 2 , 5 , 8 , 2 ])) # Output: [5, 4, 3, 4] print (reverse_sum([ 2 , 4 , 6 ])) # Output: [8, 8, 8] |
C++
#include <iostream> #include <vector> #include <cmath> using namespace std; vector< int > reverse_sum(vector< int > num) { // Convert the vector to a numerical value int n = num.size(); int num_value = 0; for ( int i = 0; i < n; i++) { num_value += num[i] * pow (10, n-i-1); } // Reverse the vector and obtain the numerical value of the reversed vector vector< int > reverse_num(num.rbegin(), num.rend()); int reverse_value = 0; for ( int i = 0; i < n; i++) { reverse_value += reverse_num[i] * pow (10, n-i-1); } // Add the two numerical values int sum_value = num_value + reverse_value; // Convert the sum to a vector of digits vector< int > result; while (sum_value > 0) { result.insert(result.begin(), sum_value % 10); sum_value /= 10; } return result; } int main() { vector< int > num1 = {2, 5, 8, 2}; vector< int > num2 = {2, 4, 6}; vector< int > result1 = reverse_sum(num1); vector< int > result2 = reverse_sum(num2); // Print the results for ( int i = 0; i < result1.size(); i++) { cout << result1[i]<< " " ; } cout << endl; for ( int i = 0; i < result2.size(); i++) { cout << result2[i]<< " " ; } cout << endl; return 0; } |
[5, 4, 3, 4] [8, 8, 8]
The time complexity of this approach is O(n), where n is the length of the input array.
The space complexity is also O(n), as we need to store the reversed array and the resulting array of digits.
Please Login to comment...