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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T05:08:20+00:00 2026-05-21T05:08:20+00:00

I have a problem and I want to make sure if I am doing

  • 0

I have a problem and I want to make sure if I am doing it most efficiently. I have an array A of float values of size N. The values are all between 0 and 1.

I have to find top k values which can be a product of a maximum of three numbers from A. So, the top-k list can
have individual numbers from A, product of two numbers or product of three numbers from A.

So, this is how I am doing it now. I can get top-k numbers in desecding order in O(Nlogk) time. I then create a
max-heap and initialize it with best values of maximum size 3 i.e. if I represent the sorted array(descending) of k values as B
and the numbers by its index in that array, I insert numbers which are at index (0), (0,1) and (0,1,2). Next, I perform extract on heap and
whenever I extract a size z (product of z numbers) value, I replace it with the set of next possible size z numbers i.e.
if suppose (2,4) is extracted, I can replace it with (3,4) and (2,5). And do extract k times to get results.

Need better ideas if you have.
Thanks all.

  • 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-21T05:08:21+00:00Added an answer on May 21, 2026 at 5:08 am

    if I understand you correctly you need to find k highest numbers that can be produced by multiplying together 1, 2 or 3 elements from your list, and all the values are floating point numbers between 0 and 1.

    It is clear that you only need to consider the k highest numbers from the list. The rest can be discarded straight away. You can use your O(n log k) algorithm to get them, again in sorted order (I assume your list isn’t preordered). To simplify the problem, you can now take their logarithms and try to maximize the sums of the numbers instead of the original problem of maximizing the products. This might speed up little.

    Now (considering the logarithmic presentation), all your numbers are negative, so adding more of them together will just create more and more negative numbers.

    Let’s call the k highest numbers A1…Ak. We can reduce the problem further now assuming that there exists also number A0, that has the value 0 in the log representation and 1 in the original representation; then the problem is to enumerate the first k 3-tuples (x,y,z in {A0,…,Ak}) with the constraint that x ≥ y ≥ z and that z < A0. Let’s denote 3-tuple by [i,j,n] and the sum of the elements in this tuple by S[i,j,n]. The first element to be reported is obviously [0,0,1], i.e. , which corresponds in the original problem formulation to the singleton #1 value on the list.

    We use a max-heap as in the original formulation; we push the triples to the heap, using their sums (S[…]) as the ordering key. The algorithm starts by pushing [0,0,0] to the heap. Then:

    answer = []
    for m in 0 .. k:
      top = heap.pop()
      answer.append(sum(top))
      (i,j,n) = top # explode the tuple
      if (n < k - 1):
          heap.push((i,j,n+1))
      if (j == n):
          heap.push((i,j+1,j+1))
          if (i == j):
              heap.push((i+1,i+1,i+1))
    

    At the end, answer contains k + 1 elements, the first one of them is [0,0,0] which must be discarded.

    Let be given as -1, -3, -8, -9. Then the algorithm proceeds like this:

    Heap
    Top          Rest (shown in order)
    
    [ 0, 0, 0] | 
    [ 0, 0,-1] | [ 0,-1,-1] [-1,-1,-1]
    [ 0,-1,-1] | [-1,-1,-1] [ 0,-1,-3] [ 0,-3,-3]
    [-1,-1,-1] | [-1,-1,-2] [ 0,-1,-3] [-1,-2,-2] [-2,-2,-2] [ 0,-3,-3]
    [-1,-1,-2] | [ 0,-1,-3] [-1,-1,-3] [-1,-2,-2] [-2,-2,-2] [ 0,-3,-3]
    [ 0,-1,-3] | [-1,-1,-3] [ 0,-1,-4] [-1,-2,-2] [-2,-2,-2] [ 0,-3,-3]
    [-1,-1,-3] | [ 0,-1,-4] [-1,-1,-4] [-1,-2,-2] [-2,-2,-2] [ 0,-3,-3]
    [ 0,-1,-4] | [-1,-2,-2] [-1,-1,-4] [ 0,-1,-5] [-2,-2,-2] [ 0,-3,-3]
    ...
    etc.
    

    The nice thing about this algorithm is that it doesn’t enumerate duplicates and the heap size is O(k); to see why, observe that the algorithm adds on every iteration the maximum of elements on the heap (often less), so after k iterations there cannot be more than 2k elements in the heap.

    This gives then running time O(n log k + k log k) = O((n + k) log k).

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

Sidebar

Related Questions

I have 3 panels and I want to make drags on them. The problem
I have problem with return statment >.< I want to store all magazine names
I have a script that I want to make sure only one is running,
I have a problem and I want to implement the MVC pattern to my
I have a problem when I want to sending data using byte format in
I have problem with fancybox. I want to write a function that will run
I have problem with HTTP headers, they're encoded in ASCII and I want to
I have problem with dynamically created image (JavaScript). I want to change the innerHTML
I want to place UItextField in UITableViewCell but I have problem. Text field don't
with my RCP program I have the problem that I want to have more

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.