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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:23:38+00:00 2026-05-12T12:23:38+00:00

Here are two integers set, say A and B and we can get another

  • 0

Here are two integers set, say A and B
and we can get another set C, in which every element is sum of element a in A and element b in B.

For example, A = {1,2}, B = {3,4} and we get C = {4, 5, 6} where 4=1+3, 5=1+4=2+3, 6=2+4

Now I want to find out which number is the kth largest one in set C, for example 5 is 2nd largest one in above example.

Is there a efficient solution?

I know that pairwise sums sorting is an open problem and has a n^2 lower time bound. But since only kth largest number is needed, maybe we can learn from the O(n) algorithm of finding median number in an unsorted array.

Thanks.

  • 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-12T12:23:39+00:00Added an answer on May 12, 2026 at 12:23 pm

    If k is very close to 1 or N, any algorithm that generates the sorted sums lazily could simply be run until the kth or N-kth item pops out.

    In particular, I’m thinking of best-first search of the following space: (a,b) means the ath item from A, the first list, added to the bth from B, the second.

    Keep in a best=lowest priority queue pairs (a,b) with cost(a,b) = A[a]+B[b].

    Start with just (1,1) in the priority queue, which is the minimum.

    Repeat until k items popped: 
     pop the top (a,b)
     if a<|A|, push (a+1,b)
     if a=1 and b<|B|, push (a,b+1)
    

    This gives you a saw-tooth comb connectivity and saves you from having to mark each (a,b) visited in an array. Note that cost(a+1,b)>=cost(a,b) and cost(a,b+1)>=cost(a,b) because A and B are sorted.

    Here’s a picture of a comb to show the successor generation rule above (you start in the upper left corner; a is the horizontal direction):

    |-------
    |-------
    |-------
    

    It’s just best-first exploration of (up to) all |A|*|B| tuples and their sums.

    Note that the most possible items pushed before popping k is 2*k, because each item has either 1 or 2 successors. Here’s a possible queue state, where items pushed into the queue are marked *:

    |--*----
    |-*-----
    *-------
    

    Everything above and to the left of the * frontier has already been popped.

    For the N-k<k case, do the same thing but with reversed priority queue order and exploration order (or, just negate and reverse the values, get the (N-k)th least, then negate and return the answer).

    See also: sorted list of pairwise sums on SO, or the Open problems project.

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

Sidebar

Related Questions

I am writing a function and I want it two return two integers as
I have a table with for columns, a smallint, an integer and two floats.
I need write a set of clauses that take a list of integer lists
I'm working on a Project Euler problem which requires factorization of an integer. I
I'm new to psuedocode, and I'm having trouble putting all the pieces together: Here
There are several questions on Stack Overflow discussing how to find the Greatest Common
I am using a subquery to return a count as an integer value to
Suppose I have the following table definition: CREATE TABLE x (i serial primary key,
I'm using Java at the core backend and JSP pages as GUI frontend. and
I'm building a CLI survey app in Java that lets users answer multiple-choice questions.

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.