Time limit: 500ms
Memory limit: 256mb
Description:
In this exercise, you are required to maintain an array of sorted integers in non-descending order and support the following operations.
1. Based on Exercise 1, your 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-descending order, before and after the merge.
2. After the merge, 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).
int Merge(int A_1[], int A_2[], int n_1, int n_2);
Example:
Given array A_1[] = [1,2,3,4,5] with n_1 = 5 and array A_2[] = [1,2,3,4,5,6] with n_2 = 6, merge A_2[] to A_1[]
Array A_1[] then becomes: [1,1,2,2,3,3,4,4,5,5,6], the function returns new length n_1’ = 11
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-descending.
Example:
Given the array A_1 = [1,1,2,2,3,3,4,4,5,5,6] with length n_1 = 11, remove the duplications
A_1 then becomes [1,2,3,4,5,6], the function returns new length n_1’ = 6
Sample input:
5
1 2 3 4 5
6
1 2 3 4 5 6
Sample output:
1 1 2 2 3 3 4 4 5 5 6
1 2 3 4 5 6
-------------------------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");
}
int Insert(int value, int A[], int n)
{
// insert value into A[], return new length n
}
int Merge(int A_1[], int A_2[], int n_1, int n_2)
{
}
int removeDuplicates(int A[], int n)
{
}
int main()
{
int A_1[100], A_2[100];
int n_1, n_2; // the number of integers in A
int value;
// Array initialization
// How many integers in FIRST array initially
scanf("%d", &n_1);
// What are the integers
int temp = 0;
for(int i=0; i<n_1; i++){
scanf("%d", &value);
temp = Insert(value, A_1, temp);
}
// How many integers in SECOND array initially
scanf("%d", &n_2);
// What are the integers
temp = 0;
for(int i=0; i<n_2; i++){
scanf("%d", &value);
temp = Insert(value, A_2, temp);
}
// Merge the SECOND array to the FIRST array
n_1 = Merge(A_1, A_2, n_1, n_2);
// print FIRST array
Print(A_1, n_1);
// remove the duplicate integers in the FIRST array
n_1 = removeDuplicates(A_1, n_1);
// print FIRST array
Print(A_1, n_1);
return 0;
}
-------------------------------------------End of Code-------------------------------------------