
- 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 elements of given arrays with given constraints?
Here we will see one problem. We will add two array elements and store them into another array. But we will follow some constraints. These constraints are like below −
- Addition should be stated from 0th index of both of the array
- Split the sum if it is more than one-digit number, and place each digit to the corresponding locations
- Remaining digits of the larger input array will be stored at output array
Let us see the algorithm to get the idea.
Algorithm
addArrayConstraints(arr1, arr2)
Begin define empty vector out i := 0 while i is less than both arr1.length and arr2.length, do add := arr1[i] + arr2[i] if add is single digit number, then insert add into out else split the digits and push each digit into out end if done while arr1 is not exhausted, do insert each element directly into out if they are single digit, otherwise split and insert done while arr2 is not exhausted, do insert each element directly into out if they are single digit, otherwise split and insert done End
Example
#include<iostream> #include<vector> using namespace std; void splitDigit(int num, vector<int> &out) { //split the digits and store into vector to add into array vector<int> arr; while (num) { arr.insert(arr.begin(), num%10); num = num/10; } out.insert(out.end(), arr.begin(), arr.end()); } void addArrayConstraints(int arr1[], int arr2[], int m, int n) { vector<int> out; int i = 0; //point current index of arr1 and arr2 while (i < m && i <n) { int add = arr1[i] + arr2[i]; if (add < 10) //if it is single digit, put the sum, otherwise split them out.push_back(add); else splitDigit(add, out); i++; } } while (i < m) //if arr1 has more elements splitDigit(arr1[i++], out); while (i < n) //if arr2 has more elements splitDigit(arr2[i++], out); for (int i = 0; i< out.size(); i++) cout << out[i] << " "; } main() { int arr1[] = {9323, 8, 6, 55, 25, 6}; int arr2[] = {38, 11, 4, 7, 8, 7, 6, 99}; int n1 = sizeof(arr1) / sizeof(arr1[0]); int n2 = sizeof(arr2) / sizeof(arr2[0]); addArrayConstraints(arr1, arr2, n1, n2); }
Output
9 3 6 1 1 9 1 0 6 2 3 3 1 3 6 9 9
- Related Articles
- Add the elements of given arrays with given constraints?
- C++ program to find last value of matrix with given constraints
- Find duplicates under given constraints in C++
- Find the longest path in a matrix with 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++
- Missing even and odd elements from the given arrays in C++
- 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
- Number of indexes with equal elements in given range in C++
- Count of matrices (of different orders) with given number of elements in C++
- How to add constraints programmatically using Swift

Advertisements