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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:50:45+00:00 2026-06-15T19:50:45+00:00

I translated the problem into an employee/salary problem for simplicity. Having an employee record

  • 0

I translated the problem into an employee/salary problem for simplicity.

Having an employee record emp such as:

| id | salary (in 1000s) |

Given a number ‘num‘, find salary ‘sal‘ where the number of employees receiving salary<=sal are >=num (similar to area under curve problems in statistics).
We are using Python and SQLite, but the problem is not specific to them:

I’m doing the following (naive starting solution):

num = some_num
sal = 1000 # starting miminmum value
count = 0
while count < num:
    sql = 'select count(*) from (select 1 from emp where salary<=? limit ?)' 
    # using limit so that we don't keep counting more than num - might help (?)
    (count,) = cursor.execute(sql, (sal, num)).next() # using apsw sqlite adapter
    sal += 1000

    print sal

How can we make this more efficient? (Algorithmically or using standard SQL or equivalent, but not using quirks of a given system)

Or else: can it be made more efficient by adding extra fields to the record that can be kept up-to-date on insert/update ops without too much overhead?

  • 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-15T19:50:46+00:00Added an answer on June 15, 2026 at 7:50 pm

    If you are using a prepared statement, I believe you can move the preparation step out of the loop to make it much faster.

    sql = 'select count(*) from (select 1 from emp where salary<=? limit ?)' 
    # using limit so that we don't keep counting more than num - might help (?)
    while count < num:
        (count,) = cursor.execute(sql, (sal, num))
        sal += 1000
    

    If you further want to improve performance and your db size is reasonably small, you could load the entire data into an array and do your operation.

    I think further optimization is possible if you sort the array by salary first. After that you can do things like binary Search to where the < condition flips and the index of that point + 1 would be the count.

    The solution is simpler than it looks. If the records are sorted by salary then the #num'th record’s salary would be the desired answer, so this becomes a problem of selecting the n’th row:

    num = some_num
    sql = 'select salary from emp order by salary limit 1 offset ?'
    (sal,) = cursor.execute(sql, (num-1,)).next()
    print sal
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im having a problem with the HeaderText not being translated when i have definied
Yesterday I read a problem which can be translated into the following problem with
I have run into this problem: Custom Methods & Extension Methods cannot be translated
This problem came up in the real world, but I've translated it into a
I have translated Jeremiah Clark's CheckBoxList Helper for MVC into my VB.Net project but
I'm using Django 1.1 on a project, and I ran into a problem. I
We have a problem here. We need to translate a website into multiple language.
We have a large flash site which is translated into 11 languages. We have
I have in the properties file several properties to be translated into different laguages:
I'm pulling my hair out because I've run into the following problem with one

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.