Time limit: 500ms
Memory limit: 256mb
Description:
In this exercise, you have two tasks:
1. The first task is to merge one integer array A_2[100] to another integer array A_1[100]. Both A_1[], A_2[] store sorted integers in non-ascending order, before and after the merge.
2. After the merge, the second task is to remove the duplicate integers in the merged array in-place such that each unique element appears only once.
You could assume that A_1[] and A_2[] are always enough to store all the integers, before and after merging (i.e. length A_1 <= 100, length A_2 <= 100, length A_1 + length A_2 <= 100).
void Merge(int A_1[], int A_2[], int n_1, int n_2);
Example:
Given array A_1[] = [6,5,4,3,2,1] with n_1 = 6 and array A_2[] = [5,4,3,2,1] with n_2 = 5, merge A_2[] to A_1[]
Array A_1[] then becomes: [6,5,5,4,4,3,3,2,2,1,1].
int removeDuplicates(int A[], int n)
Given the integer array A_1[] after the merge, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept in non-ascending.
Example:
Given the array A_1 = [6,5,5,4,4,3,3,2,2,1,1] with length n_1 = 11, remove the duplications
A_1 then becomes [6,5,4,3,2,1], the function returns new length n_1’ = 6
Sample input:
6
6 5 4 3 2 1
5
5 4 3 2 1
Sample output:
6 5 5 4 4 3 3 2 2 1 1
6 5 4 3 2 1
-------------------------Copy the following code, complete it and submit-------------------------
#include <stdio.h>
void Print(int A[], int n)
{
// print A[0], A[1], ..., A[n-1] in one line, every two integers are separated by a space
for(int i=0; i<n; i++)
printf("%d ", A[i]);
printf("\n");
}
void Merge(int A_1[], int A_2[], int n_1, int n_2)
{
// WRITE YOUR CODE HERE
}
int removeDuplicates(int A[], int n)
{
// WRITE YOUR CODE HERE
}
int main()
{
int A_1[100], A_2[100];
int n_1, n_2; // the number of integers in A_1 and A_2
int value;
// Array initialization
// How many integers in FIRST array initially
scanf("%d", &n_1);
// What are the integers
for(int i=0; i<n_1; i++){
scanf("%d", &value);
A_1[i] = value;
}
// How many integers in SECOND array initially
scanf("%d", &n_2);
// What are the integers
for(int i=0; i<n_2; i++){
scanf("%d", &value);
A_2[i] = value;
}
// Merge the SECOND array to the FIRST array
Merge(A_1, A_2, n_1, n_2);
// print FIRST array
Print(A_1, n_1 + n_2);
// remove the duplicate integers in the FIRST array
n_1 = removeDuplicates(A_1, n_1 + n_2);
// print FIRST array
Print(A_1, n_1);
return 0;
}
-------------------------------------------End of Code-------------------------------------------