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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:19:06+00:00 2026-05-19T02:19:06+00:00

This is a homework question, binary search has already been introduced: Given two arrays,

  • 0

This is a homework question, binary search has already been introduced:

Given two arrays, respectively N and M elements in ascending order, not necessarily unique:
What is a time efficient algorithm to find the kth smallest element in the union of both arrays?

They say it takes O(logN + logM) where N and M are the arrays lengths.

Let’s name the arrays a and b. Obviously we can ignore all a[i] and b[i] where i > k.
First let’s compare a[k/2] and b[k/2]. Let b[k/2] > a[k/2]. Therefore we can discard also all b[i], where i > k/2.

Now we have all a[i], where i < k and all b[i], where i < k/2 to find the answer.

What is the next step?

  • 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-19T02:19:07+00:00Added an answer on May 19, 2026 at 2:19 am

    You’ve got it, just keep going! And be careful with the indexes…

    To simplify a bit I’ll assume that N and M are > k, so the complexity here is O(log k), which is O(log N + log M).

    Pseudo-code:

    i = k/2
    j = k - i
    step = k/4
    while step > 0
        if a[i-1] > b[j-1]
            i -= step
            j += step
        else
            i += step
            j -= step
        step /= 2
    
    if a[i-1] > b[j-1]
        return a[i-1]
    else
        return b[j-1]
    

    For the demonstration you can use the loop invariant i + j = k, but I won’t do all your homework 🙂

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

Sidebar

Related Questions

Disclaimer: This is for a homework assignment, but the question is not regarding the
This is a homework question I am sorry but I am lost. What happens
This is not a homework question, just something at work that's bugging me. I'm
I have been pondering about my homework question for a while. I welcome (and
This isnt a homework question. I took data structures at a Community College and
This is a homework question, so I would like to avoid complete answers and
For starters this is a homework question. I have an idea but I am
(This is no homework and no work issue. It's just my personal interest/occupation and
This one is a case of not doing your homework.:-) Apart from dynamic loading
I'm doing a basic homework assignment which looks like this: While input <> -1

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.