
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Add the elements of given arrays with given constraints?
For this problem, to add elements of two given arrays we have some constraints based on which the added value will get changed. the sum of two given arrays a[] & b[] is stored into to third array c[]in such a way that they gave the some of the elements in single digit. and if the number of digits of the sum is greater than 1, then the element of the third array will split into two single-digit elements. For example, if the sum occurs to be 27, the third array with store it as 2,7.
Input: a[] = {1, 2, 3, 7, 9, 6} b[] = {34, 11, 4, 7, 8, 7, 6, 99} Output: 3 5 1 3 7 1 4 1 7 1 3 6 9 9
Explanation
Output array and run a loop from the 0th index of both arrays. For each iteration of the loop, we consider the next elements in both arrays and add them. If the sum is greater than 9, we push the individual digits of the sum to output array else we push the sum itself. Finally, we push the remaining elements of the larger input array to the output array.
Example
#include <iostream> #include<bits/stdc++.h> using namespace std; void split(int n, vector<int> &c) { vector<int> temp; while (n) { temp.push_back(n%10); n = n/10; } c.insert(c.end(), temp.rbegin(), temp.rend()); } void addArrays(int a[], int b[], int m, int n) { vector<int> out; int i = 0; while (i < m && i < n) { int sum = a[i] + b[i]; if (sum < 10) { out.push_back(sum); } else { split(sum, out); } i++; } while (i < m) { split(a[i++], out); } while (i < n) { split(b[i++], out); } for (int x : out) cout << x << " "; } int main() { int a[] = {1, 2, 3, 7, 9, 6}; int b[] = {34, 11, 4, 7, 8, 7, 6, 99}; int m =6; int n = 8; addArrays(a, b, m, n); return 0; }
- Related Articles
- Add elements of given arrays with given constraints?
- C++ program to find last value of matrix with given constraints
- Find the longest path in a matrix with given constraints in C++
- Missing even and odd elements from the given arrays in C++
- Find duplicates under given constraints in C++
- Find minimum time to finish all jobs with given constraints in Python
- Find minimum time to finish all jobs with given constraints in C++
- Maximum number of ones in a N*N matrix with given constraints in C++
- Find three closest elements from given three sorted arrays in C++
- Find Values after N Operations to Remove N Characters of String ‘S’ With Given Constraints
- Divide a given scalar element with masked array elements and return arrays with Quotient and Remainder in NumPy
- Divide masked array elements by a given scalar element and return arrays with Quotient and Remainder in NumPy
- Check if it is possible to convert one string into another with given constraints in Python
- Python Program for Number of elements with odd factors in the given range
- Number of indexes with equal elements in given range in C++
