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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:21:27+00:00 2026-06-13T20:21:27+00:00

I have a small algorithm problem. I have an array for example. array[10] =

  • 0

I have a small algorithm problem. I have an array for example.

array[10] = {23,54,10,63,52,36,41,7,20,22};

now given an input number for example 189 i want to know that in which slot it should lie.
for example this input should lie in 4 index in the array because

23+54+10+63 = 150 and if we add 52 then sum will be 202 which will cover the range where 189 should lie. so the answer should be 4. 

I want to find an amortized constant time algorithm may be in the first step we do some prepossessing on the array so that all the next queries we can get in constant time.

The input number will always be in between 1 and sum of all the entries in 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-06-13T20:21:29+00:00Added an answer on June 13, 2026 at 8:21 pm

    The natural solution would be to first build an array with the cumulative sums. This would look like

    sums[10] = {23,77,87,...}
    

    and then use a binary search such as the lower_bound algorithm to find where to insert. That would be O(log(n)). Assuming that your number of slots is constant, this solution is also time-constant. But I guess you want the lookup to be O(1) in terms of the number of slots. In this case, you will have to make a full lookup table. Since the size of these numbers is relatively small, that is perfectly doable:

    int lookup[N];
    for(i=0,j=0;i<10;i++)
        for(k=0;k<sums[i];k++,j++)
            lookup[j]=i;
    

    Using this, the slot number is simply lookup[number].

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

Sidebar

Related Questions

I have a small algorithm which replaces the position of characters in a String:
I'm working on an algorithm that has to do a small number of operations
I have actually a small problem with file i/o and finding the right algorithm
have small problem, and would very much appreciate help :) I should convert byte
I have small script in bash, which is generating graphs via gnuplot. Everything works
I have small problem with Spring MVC. Basically what I'm trying to do is
I have small web app that generate PDF files as a report. I'm trying
My problem is that I have an image of a detailed street map. On
I've reduced my problem (table layout algorithm) to the following problem: Imagine I have
So here is my problem: I have an image, that image is large (high

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.