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

  • SEARCH
  • Home
  • 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 8897609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:23:24+00:00 2026-06-15T00:23:24+00:00

I have two lists made of numbers(integers); both have 2 million unique elements. I

  • 0

I have two lists made of numbers(integers); both have 2 million unique elements.

I want to find number a from list 1 and b from list 2, that –

1)a*b should be maximized.
2)a*b has to be smaller than certain limit.

here’s what I came up with:

maxpq = 0
nums = sorted(nums, reverse=True)
nums2 = sorted(nums2, reverse=True)
for p in nums:
    n = p*dropwhile(lambda q: p*q>sqr, nums2).next()
    if n>maxpq:
        maxpq=n
print maxpq

any suggestions?
edit : my method is too slow. It would take more than one day.

  • 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-06-15T00:23:25+00:00Added an answer on June 15, 2026 at 12:23 am

    Here’s a linear-time solution (after sorting):

    def maximize(a, b, lim):
        a.sort(reverse=True)
        b.sort()
        found = False
        best = 0
        j = 0
        for i in xrange(len(a)):
            while j < len(b) and a[i] * b[j] < lim:
                found = True
                if a[i]*b[j] > best:
                    best, n1, n2 = a[i] * b[j], a[i], b[j]
                j += 1
        return found and (best, n1, n2)
    

    Simply put:

    • start from the highest and lowest from each list
    • while their product is less than the target, advance the small-item
    • once the product becomes bigger than your goal, advance the big-item until it goes below again

    This way, you’re guaranteed to go through each list only once. It’ll return False if it couldn’t find anything small enough, otherwise it’ll return the product and the pair that produced it.

    Sample output:

    a = [2, 5, 4, 3, 6]
    b = [8, 1, 5, 4]
    maximize(a, b, 2)   # False
    maximize(a, b, 3)   # (2, 2, 1)
    maximize(a, b, 10)  # (8, 2, 4)
    maximize(a, b, 100) # (48, 6, 8)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists. They are sortable but not connected (elements of list one
We have two lists: a=['1','2','3','4'] b=['2','3','4','5'] How to get a list with elements that
I want to calculate , in two lists (same lenght), the number of elements
I have made a list view containing two childs. Now i am trying to
I have two Lists of dictionaries. Both are not not null. How to merge
I have two Lists, they look like this <List> ads [0] Headline = Sony
I have two lists. Ex: List<string> expected = new List<string>(); expected.Add( a ); expected.Add(
I have two lists: [a, b, c] [d, e, f] I want: [a, d,
I have two lists which are guaranteed to be the same length. I want
i have two lists: List<comparerobj> list_c = new List<comparerobj>(); List<comparerobj> list_b = new List<comparerobj>();

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.