A
A
A_Htke2020-12-11 19:33:59
C++ / C#
A_Htke, 2020-12-11 19:33:59

C: how to find the difference of two arrays using qsort?

Given two arrays of numbers A and B. It is required to find all such values ​​of the elements of array A, which are not among the elements of array B. In the problem, it is necessary to use the qsort function.
Signature:
The first line contains an integer N (1<=N<=10^5) - the number of elements of the array A. The second line contains N integers separated by a space, each of which does not exceed 10^9 in absolute value - the elements of the array A. The next two lines contain the elements of array B in a similar format. In the first line, you need to print one integer - the number of values ​​that satisfy the described condition. In the second line you need to display all such values ​​in ascending order.
Only code without qsort comes to my mind. Work works, but only without the desired function. Naturally, the program crashes on one of the tests in the system. Please tell me how to shove qsort here:

#include<stdio.h>
int main()
{
    int N;
    int A[100001] = {0}; 
    scanf("%d", &N);
    for (int i = 0, k; i < N; i++)
    {
        scanf("%d", &k);
        A[k] = 1;
    }
    scanf("%d", &N);  
    for (int i = 0, k; i < N; i++)
    {
        scanf("%d", &k);
        A[k] = 0;  
    }
    N = 0;
    for (int i = 0; i <= 100000; i++)
        N += A[i];
    printf("%d\n", N);
    for (int i = 0; i <= 100000; i++)
        if (A[i]) 
            printf("%d ", i);
    puts(""); //в списке нет языка С, поэтому код просто текстом
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rsa97, 2020-12-11
@Rsa97

Sort both arrays, then iterate through both arrays at once, looking for elements that differ.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question