给定两个整数数组,通过满足以下约束将其元素添加到第三个数组中—— 1.应从两个数组的第0个索引开始添加。 2.如果总和不是一位数字,则将其拆分,并将数字存储在输出数组中的相邻位置。 3.输出数组应容纳较大输入数组的任何剩余数字。
null
例如:
Input: a = [9, 2, 3, 7, 9, 6]b = [3, 1, 4, 7, 8, 7, 6, 9]Output: [1, 2, 3, 7, 1, 4, 1, 7, 1, 3, 6, 9]Input: a = [9343, 2, 3, 7, 9, 6]b = [34, 11, 4, 7, 8, 7, 6, 99]Output: [9, 3, 7, 7, 1, 3, 7, 1, 4, 1, 7, 1, 3, 6, 9, 9]Input: a = []b = [11, 2, 3 ]Output: [1, 1, 2, 3 ]Input: a = [9, 8, 7, 6, 5, 4, 3, 2, 1]b = [1, 2, 3, 4, 5, 6, 7, 8, 9]Output: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0]
难度等级: 菜鸟
这个想法很简单。我们维护一个输出数组,并从两个数组的第0个索引运行一个循环。对于循环的每个迭代,我们考虑两个数组中的下一个元素并添加它们。如果总和大于9,我们将总和的各个数字推送到输出数组,否则我们将推总和本身。最后,我们将较大的输入数组的剩余元素推送到输出数组。
以下是上述理念的实施:
C++
// C++ program to add two arrays following given // constraints #include<bits/stdc++.h> using namespace std; // Function to push individual digits of a number // to output vector from left to right void split( int num, vector< int > &out) { vector< int > arr; while (num) { arr.push_back(num%10); num = num/10; } // reverse the vector arr and append it to output vector out.insert(out.end(), arr.rbegin(), arr.rend()); } // Function to add two arrays keeping given // constraints void addArrays( int arr1[], int arr2[], int m, int n) { // create a vector to store output vector< int > out; // maintain a variable to store current index in // both arrays int i = 0; // loop till arr1 or arr2 runs out while (i < m && i < n) { // read next elements from both arrays and // add them int sum = arr1[i] + arr2[i]; // if sum is single digit number if (sum < 10) out.push_back(sum); else { // if sum is not a single digit number, push // individual digits to output vector split(sum, out); } // increment to next index i++; } // push remaining elements of first input array // (if any) to output vector while (i < m) split(arr1[i++], out); // push remaining elements of second input array // (if any) to output vector while (i < n) split(arr2[i++], out); // print the output vector for ( int x : out) cout << x << " " ; } // Driver code int main() { int arr1[] = {9343, 2, 3, 7, 9, 6}; int arr2[] = {34, 11, 4, 7, 8, 7, 6, 99}; int m = sizeof (arr1) / sizeof (arr1[0]); int n = sizeof (arr2) / sizeof (arr2[0]); addArrays(arr1, arr2, m, n); return 0; } |
JAVA
// Java program to add two arrays following given // constraints import java.util.Vector; class GFG { // Function to push individual digits of a number // to output vector from left to right static void split( int num, Vector<Integer> out) { Vector<Integer> arr = new Vector<>(); while (num > 0 ) { arr.add(num % 10 ); num /= 10 ; } // reverse the vector arr and // append it to output vector for ( int i = arr.size() - 1 ; i >= 0 ; i--) out.add(arr.elementAt(i)); } // Function to add two arrays keeping given // constraints static void addArrays( int [] arr1, int [] arr2, int m, int n) { // create a vector to store output Vector<Integer> out = new Vector<>(); // maintain a variable to store // current index in both arrays int i = 0 ; // loop till arr1 or arr2 runs out while (i < m && i < n) { // read next elements from both arrays // and add them int sum = arr1[i] + arr2[i]; // if sum is single digit number if (sum < 10 ) out.add(sum); else // if sum is not a single digit number, // push individual digits to output vector split(sum, out); // increment to next index i++; } // push remaining elements of first input array // (if any) to output vector while (i < m) split(arr1[i++], out); // push remaining elements of second input array // (if any) to output vector while (i < n) split(arr2[i++], out); // print the output vector for ( int x : out) System.out.print(x + " " ); } // Driver Code public static void main(String[] args) { int [] arr1 = { 9343 , 2 , 3 , 7 , 9 , 6 }; int [] arr2 = { 34 , 11 , 4 , 7 , 8 , 7 , 6 , 99 }; int m = arr1.length; int n = arr2.length; addArrays(arr1, arr2, m, n); } } // This code is contributed by // sanjeev2552 |
C#
// C# program to add two arrays following given // constraints using System; using System.Collections.Generic; class GFG { // Function to push individual digits of a number // to output vector from left to right static void split( int num, List< int > outs) { List< int > arr = new List< int >(); while (num > 0) { arr.Add(num % 10); num /= 10; } // reverse the vector arr and // append it to output vector for ( int i = arr.Count - 1; i >= 0; i--) outs.Add(arr[i]); } // Function to add two arrays keeping given // constraints static void addArrays( int [] arr1, int [] arr2, int m, int n) { // create a vector to store output List< int > outs = new List< int >(); // maintain a variable to store // current index in both arrays int i = 0; // loop till arr1 or arr2 runs out while (i < m && i < n) { // read next elements from both arrays // and add them int sum = arr1[i] + arr2[i]; // if sum is single digit number if (sum < 10) outs.Add(sum); else // if sum is not a single digit number, // push individual digits to output vector split(sum, outs); // increment to next index i++; } // push remaining elements of first input array // (if any) to output vector while (i < m) split(arr1[i++], outs); // push remaining elements of second input array // (if any) to output vector while (i < n) split(arr2[i++], outs); // print the output vector foreach ( int x in outs) Console.Write(x + " " ); } // Driver Code public static void Main(String[] args) { int [] arr1 = { 9343, 2, 3, 7, 9, 6 }; int [] arr2 = { 34, 11, 4, 7, 8, 7, 6, 99 }; int m = arr1.Length; int n = arr2.Length; addArrays(arr1, arr2, m, n); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Javascript program to add two arrays // following given constraints // Function to push individual digits // of a number to output vector from // left to right function split(num, out) { let arr = []; while (num) { arr.push(num % 10); num = Math.floor(num / 10); } for (let i = arr.length - 1; i >= 0; i--) out.push(arr[i]); } // Function to add two arrays keeping given // constraints function addArrays(arr1, arr2, m, n) { // Create a vector to store output let out = []; // Maintain a variable to store // current index in both arrays let i = 0; // Loop till arr1 or arr2 runs out while (i < m && i < n) { // Read next elements from both // arrays and add them let sum = arr1[i] + arr2[i]; // If sum is single digit number if (sum < 10) out.push(sum); else { // If sum is not a single digit // number, push individual digits // to output vector split(sum, out); } // Increment to next index i++; } // Push remaining elements of first // input array (if any) to output vector while (i < m) split(arr1[i++], out); // Push remaining elements of second // input array (if any) to output vector while (i < n) split(arr2[i++], out); // Print the output vector for (let x of out) document.write(x + " " ); } // Driver code let arr1 = [ 9343, 2, 3, 7, 9, 6 ]; let arr2 = [ 34, 11, 4, 7, 8, 7, 6, 99 ]; let m = arr1.length; let n = arr2.length; addArrays(arr1, arr2, m, n); // This code is contributed by _saurabh_jaiswal </script> |
输出:
9 3 7 7 1 3 7 1 4 1 7 1 3 6 9 9
时间复杂性 上面的解决方案是O(m+n),因为我们只遍历两个数组一次。
本文由 阿迪蒂亚·戈尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END