Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6058631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:37:14+00:00 2026-05-23T08:37:14+00:00

I have three one-dimensional arrays. The task is to store the numbers which exist

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:37:14+00:00Added an answer on May 23, 2026 at 8:37 am

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a multi-dimensional array, which basically consists of one sub-array for each year.
I have three arrays that need to be combined in one three-dimension array. The
I'm working in VS 2008 and have three projects in one solution. I'm debugging
I have an array in Perl: my @my_array = (one,two,three,two,three); How do I remove
I have a logging table which has three columns. One column is a unique
I have a multi dimensional array in PHP. $f = array('one' => array(*doesntmatter*), two
I'm working with VB.Net, and have two one-dimensional Arrays. Is there an Inbuilt function
I have three classes, TImageProcessingEngine , TImage and TProcessing TImageProcessingEngine is the one which
Okay, I have a multi-dimensional array which is statically-allocated. I'd very much like to
I have a three dimensional static const array that is acting as a lookup

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.