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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:04:09+00:00 2026-05-27T12:04:09+00:00

Question is simple, I want to map every number from 0 to N-1 to

  • 0

Question is simple, I want to map every number from 0 to N-1 to a number of elements K < N such that:
1,2,3,…,i-1 maps to element 1
i, i+1, i+2,…,i+k-1 maps to element 2
… and so on until
i+k+…+z, i+k+…+z+1, i+k+…+z+2, …, N-1 maps to element K

Note: i,j, k,…,z are different values (otherwise I wouldn’t have used different letters 🙂 ).

Is there a way to have a structure and a function f(i) returning the corresponding element in time O(1) occupying a reasonable amount of space? (A vector of N elements with each element from the range pointing to the element it affects is NOT a reasonable amount of space)

I can think of B trees which would get me a O(log(n)) access time, but I’m curious if there’s a O(1) efficient solution.

Thanks in advance!

Bruno

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

    If your i,k,…,z are arbitrary, then no algorithm with a time constraint of O(1) and a reasonable space constraint of < O(N) comes to mind. If there is some regularity in your bounds, then of course you can put that into use: e.g. if each range contains an even number of elements, then a simple solution be to have a lookup table that is acccessed using [i_N/2]. In the general case, you would need to have a matching folding-function for your number 0…N – one that satisfies the constraint k-mapping(i) != k-mapping(i+1) => folding-function(i) != folding-function(i+1).

    If only some elements in your map are accessed frequently, you could also build a cache for the mapping, leading to O(1) in the cache-hit case and O(lookup-function) for the cache-miss. Of course, the cache lookup would impact the time spent for each element, even in case of a cache-miss, but it would not affect the O-complexity of your algorithm.

    Edit

    Basically what you want to implement can be expressed as a Pascal case statement with sets as labels:

    case n of 
    0..i-1:
      value:=0;
    i..i+k-1:
      value:=1;
    // ...
    i+k+...+z+2..N-1:
      value:=K;
    end
    

    For this problem, a Google search indeed turns out an interesting paper:

    This paper descripts a new scheme for building static search trees,
    using multiway radix search trees. […] We show that, for sparse case
    sets, the method produces faster code on average than existing
    methods, requiring O(1) time with a small constant for the average
    search.

    Erlingsson, Krishnamoorthy, Raman: Lucid and Efficient Case Analysis

    Alas, you don’t have a sparse case set, but as far as I can see, the algorithm should require only minimal adaption for dense case sets that have lots of consecutive results.

    For the general problem of creating efficient code for case statements, Zhao and Amarla have an interesting paper that uses profiling to achieve something similar to using a cache. Their paper is also interesting for the related work section, which refers to a paper from Kannan and Proebsting (Correction to producing good code for the case statement.) which requires an O(n^2) setup time for clustering. However, I have not access to this paper, and it sounds like that this would only produce an extremely optimized search tree, therefore leading to a runtime of O(log(n)).

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

Sidebar

Related Questions

Well this is a simple question i want to filter two elements sorted reverse
Really simple question: From within GWT I want to forward the user away from
Simple question, I just want to select the text from the <Template> tag. Here's
I have a simple question. I have a long std::string that I want to
Really simple question hopefully. I want to do something like this: Map<String, String> temp
Simple question, I just want to initialize a map to be empty, instead of
A simple question, I think: I want to put a tag into an ASP.NET
Simple question I guess, I want to use PHP to write an update to
Very simple question, but I want to start using a consistent naming convention for
Simple question: I am opening a file in matlab 7.x, and I want to

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.