Function mergeEven: The function/method mergeEven accepts four arguments M, arr1, N and arr2. M represents the size of the integer array arr1. N represents the size of the integer array arr2. The array arr2 contains only even integers and the size of arr2 is equal to the number of even integers in arr1.
The function/method mergeEven must form a new array by merging the given two arrays so that each integer in the array arr2 occurs after the occurrence of the corresponding even integer in the array arr1. Then the function must return the new array.
Your task is to implement the function mergeEven so that it passes all the test cases.
IMPORTANT: Do not write the main() function as it is already defined.
Example Input/Output 1:
Input:
6 4
12 45 78 56 23 10
100 200 300 400
Output:
Array 1: 12 45 78 56 23 10
Array 2: 100 200 300 400
Merged Array: 12 100 45 78 200 56 300 23 10 400
Explanation:
Here M = 6 and N = 4.
The integers in the 1st array are 12, 45, 78, 56, 23 and 10.
The integers in the 2nd array are 100, 200, 300 and 400.
After merging the two arrays based on the given conditions, the merged array contains
12 100 45 78 200 56 300 23 10 400
100 occurs after the 1st even integer in the first array.
200 occurs after the 2nd even integer in the first array.
300 occurs after the 3rd even integer in the first array.
400 occurs after the 4th even integer in the first array.
Example Input/Output 2:
Input:
7 2
50 63 87 66 3 83 35
48 32
Output:
Array 1: 50 63 87 66 3 83 35
Array 2: 48 32
Merged Array: 50 48 63 87 66 32 3 83 35
include <stdio.h>
include <stdlib.h>
int* mergeEven(int M, int *arr1, int N, int *arr2)
{
int arr = malloc(sizeof(int)(M+N));
int i=0;
int j=0;
int count = 0;
while(count<M+N){
if(arr1[i]%2==0){
arr[count++] = arr1[i];
i++;
arr[count++] = arr2[j];
j++;
}else{
arr[count++] = arr1[i];
i++;
}
}
return arr;
}
int main()
{
int M, N;
scanf("%d %d", &M, &N);
int arr1[M], arr2[N];
for(int index = 0; index < M; index++)
{
scanf("%d", &arr1[index]);
}
for(int index = 0; index < N; index++)
{
scanf("%d", &arr2[index]);
}
int *mergedArr = mergeEven(M, arr1, N, arr2);
printf("Array 1: ");
for(int index = 0; index < M; index++)
{
printf("%d ", arr1[index]);
}
printf("nArray 2: ");
for(int index = 0; index < N; index++)
{
printf("%d ", arr2[index]);
}
printf("nMerged Array: ");
for(int index = 0; index < M+N; index++)
{
printf("%d ", mergedArr[index]);
}
return 0;
}