1

I have three one-dimensional arrays. The task is to store the numbers which exist in each of the three arrays in a forth array. Here is my solution which as you see isn't correct. I'm also interested in a faster algorithm if possible because it's O(N3) difficulty.

    #include <stdio.h>
main(){
int a[5]={1,3,6,7,8};
int b[5]={2,5,8,7,3};
int c[5]={4,7,1,3,6};
int i,j,k;
int n=0;
int d[5];
for(k=0; k<5; k++){
    for(j=0; j<5; j++){
        for(i=0; i<5; i++){
            if(a[i]==b[j] && b[j]==c[k])
                {d[n]=a[i];
                n++;}
            else
                d[n]=0;
    }}}
//Iterate over the new array
for(n=0;n<5;n++)
    printf("%d\n",d[n]);
return 0;
}

4 Answers 4

7

One way to improve to O(n log n) is to sort all three arrays first. Then use three pointers one for each array. You always move the one that points to the lowest value and after every such move check whether the three values are the same.

To improve even further you can use hashtable. Iterate through the first array and put it's values in a hashtable as keys. Then iterate through the second array and every time when the value exists as a key in the first hashtable, put it in a second one. Finally iterate over the third array and if a value exists in the second hashtable as a key store it in the forth array. This is O(n) assuming the hashtable operations are O(1).

Sign up to request clarification or add additional context in comments.

1 Comment

You could get away just with two arrays sorted btw. If you have one of the arrays much shorter than the others - it'll speed up things significantly. See my answer for details.
3

Your mistake is that you're using one of your three nested counters (which are being used to index the input arrays) as the index into the output array. You need to have a fourth index (let's call it n), which starts at zero, only increments every time a satisfactory value has been found.

2 Comments

I changed the code as you suggested but there is some problem
@George: You can edit your question to include your updated code! I can't read it when it's in a comment...
3

Sort second and third arrays beforehand and use binary search on them to determine is some element is present. If element is present in all of your arrays - it will present in the first. So, go through first (unsorted) array and check if its element is in second and third.

If you take the shortest array as the first - it will make algorithm slightly faster too.

Comments

1

You did't store them on d[] the right way.

Once found you can skip the rest of a[] and b[] for that element of c[].

#include <stdio.h>
main(){
int a[5]={1,3,6,7,8};
int b[5]={2,5,8,7,3};
int c[5]={4,7,1,3,6};
int i,j,k;
int n=0;
int found;
int d[5];
for(k=0; k<5; k++){
    found=0;
    for(j=0; j<5 && !found; j++){
        if (b[j]==c[k]) {
            for(i=0; i<5 && !found; i++){
                if(a[i]==b[j]) {
                    d[n++]=c[k];
                    found=1;
                }
            }
    }}}
//Iterate over the new array
for(i=0;i<5;i++)
    printf("%d\n",d[i]);
return 0;
}

2 Comments

It's not a better algorithm but I think I fixed your code and it's slightly faster.
I added if (b[j]==c[k]) { as a optimization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.