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 844811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:20:47+00:00 2026-05-15T06:20:47+00:00

i have seen a few days ago such problem there is given two array

  • 0

i have seen a few days ago such problem

there is given two array find      elements which are common   of these array

one of the solution was sort big array and then use binary search algorithm
and also there is another algorithm- brute-force algorithm

 for (int i=0;i<array1.length;i++){
  for (int j=0;j<array2.length;j++){
 if (array1[i]==array2[j]){
//code here
}
}

it’s complexity is O(array1.lengtharray2.length);
and i am interested the first method’s complexity is also same yes?
because we should sort array first and then use search method
binary search algorithm’s complexity is log_2(n) so it means that total time will be
array.length
log_2(n) and about sort?
please explain me which is better

  • 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-15T06:20:48+00:00Added an answer on May 15, 2026 at 6:20 am

    An O(M log N) solution

    Let the length of arr1 be O(M), and the length of arr2 be O(N). The sort/binary search algorithm is O(M log N).

    The pseudocode is as follows:

    SORT(arr2)   # N log N
    
    FOR EACH element x OF arr1             # M
       IF binarySearch(x, arr2) is FOUND   # log N
           DECLARE DUP x
    

    O(M log N) is vastly better than O(MN).


    A linear-time solution

    There’s also a third way which is O(M+N), using a set that has a O(1) insertion and test. A hash-based set meets this expectation.

    The pseudocode is as follows:

    INIT arr1set AS emptySet
    
    FOR EACH element x OF arr1    # M
       INSERT x INTO arr1set      # 1
    
    FOR EACH element x OF arr2    # N
       IF arr1set CONTAINS x      # 1
          DECLARE DUP x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 415k
  • Answers 415k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the general case, you can't reuse the existing ref… May 15, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer Found that the question is closely related to: this I… May 15, 2026 at 9:09 am
  • Editorial Team
    Editorial Team added an answer Try adding 'allDay': false to your json. I had the… May 15, 2026 at 9:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.