Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Add elements of given arrays with given constraints?
Here we will see a problem where we add two array elements and store them into another array following specific constraints. These constraints are −
- Addition should be started from 0th index of both arrays
- Split the sum if it is more than a single-digit number, and place each digit to the corresponding locations
- Remaining digits of the larger input array will be stored in the output array
Syntax
void addArrayConstraints(int arr1[], int arr2[], int m, int n); void splitDigit(int num, int* out, int* size);
Algorithm
addArrayConstraints(arr1, arr2)
Begin
define empty array 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 <stdio.h>
void splitDigit(int num, int* out, int* size) {
int temp[10], count = 0;
if (num == 0) {
out[(*size)++] = 0;
return;
}
while (num > 0) {
temp[count++] = num % 10;
num = num / 10;
}
for (int i = count - 1; i >= 0; i--) {
out[(*size)++] = temp[i];
}
}
void addArrayConstraints(int arr1[], int arr2[], int m, int n) {
int out[100];
int size = 0;
int i = 0;
while (i < m && i < n) {
int add = arr1[i] + arr2[i];
if (add < 10) {
out[size++] = add;
} else {
splitDigit(add, out, &size);
}
i++;
}
while (i < m) {
if (arr1[i] < 10) {
out[size++] = arr1[i];
} else {
splitDigit(arr1[i], out, &size);
}
i++;
}
while (i < n) {
if (arr2[i] < 10) {
out[size++] = arr2[i];
} else {
splitDigit(arr2[i], out, &size);
}
i++;
}
printf("Result: ");
for (int j = 0; j < size; j++) {
printf("%d ", out[j]);
}
printf("
");
}
int 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]);
printf("Array 1: ");
for (int i = 0; i < n1; i++) {
printf("%d ", arr1[i]);
}
printf("
");
printf("Array 2: ");
for (int i = 0; i < n2; i++) {
printf("%d ", arr2[i]);
}
printf("
");
addArrayConstraints(arr1, arr2, n1, n2);
return 0;
}
Output
Array 1: 9323 8 6 55 25 6 Array 2: 38 11 4 7 8 7 6 99 Result: 9 3 6 1 1 9 1 0 6 2 3 3 1 3 6 9 9
How It Works
- Step 1: Add corresponding elements from both arrays (9323+38=9361, 8+11=19, etc.)
- Step 2: Split multi-digit sums into individual digits (9361 ? 9,3,6,1 and 19 ? 1,9)
- Step 3: Process remaining elements from the longer array
- Step 4: Store all results in the output array
Conclusion
This algorithm efficiently handles array addition with digit splitting constraints. It ensures that all multi-digit numbers are broken down into individual digits while preserving the order of operations.
Advertisements
