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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:10:56+00:00 2026-05-30T13:10:56+00:00

suppose you are given the following problem. You have two index sets that have

  • 0

suppose you are given the following problem. You have two index sets that have a one-to-one mapping. For simplicity, let say, you have an array like int a [] = {21, 30, 45, 78} this list maps {1, 2, 3, 4} to {21, 30, 45, 78}. What is the most efficient way to obtain the reverse mapping, i.e. given index 30 you’d want the algorithm to return 2 for 45, you’d want 3 and so on. I can think of the following:

  1. A binary search for the index. This is memory efficient and has complexity O(log n).

  2. Have an array that has 79 elements and have reverseMap[21] = 1, reverseMap[30] = 2, reverseMap[45] = 3, reverseMap[78] = 4. This is O(1) and thus faster but is not memory efficient.

For my application both memory and speed are important. I’m short of memory since this is a number crunching code and thus will work with hundreds of millions of points. Speed is also important since the algorithm will be called many many times.

I sense Hash tables are useful here but I don’t know much about it to comment. I’d appreciate any insight on the problem. Also, since the coding is done in c++ I’d like to see methods that utilize STL and not external libraries.

  • 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-30T13:10:57+00:00Added an answer on May 30, 2026 at 1:10 pm

    As always: PROFILE. We can guess, but without running your code, we might be wrong. I made a rough benchmark on ideone (times are based on my computer). I did one hundred thousand lookups of unsigned int in an array with ten million elements (I got bored waiting for your “hundreds of millions”), and these were my results:

    unsorted vector  found 1633382974 in 2140 ticks.
    sorted vector  found 1633382974 in 62 ticks.
    unordered_map  found 1633382974 in 16 ticks.
    std::map  found 1633382974 in 172 ticks. //that's half the time of a blink
    

    However I have to note, maintaining these in the memory of your program will have some overhead over the unsorted vector. If we add the creation time to the timings of the hundred thousand lookups, we get:

    unsorted vector  found 1633382974 in 2141 ticks.
    sorted vector  found 1633382974 in 1797 ticks.
    unordered_map  found 1633382974 in 16218 ticks.
    std::map  found 1633382974 in 30749 ticks. //a full thirty seconds
    

    So, as you can see, the timings depend entirely on what you do in your code. Try different things, time them with optimizations on, and go with the fastest for your code.

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

Sidebar

Related Questions

Regarding this post and this other one . Suppose I have the following: public
Given that I have the following widget (and a view attached to it): public
I was asked following question in interview recently: Let suppose you have, following grid
Suppose I was given a URL. It might already have GET parameters (e.g. http://example.com/search?q=question
I have recently come across an interesting question on strings. Suppose you are given
Suppose that my Haskell function is given an input, which is supposed to be
In Programming Pearls I have met the following problem. The question is this: print
Suppose I have the following tables in my database: Now all my queries depend
The Test Cover problem can be defined as follows: Suppose we have a set
Suppose I have a file with the following marks (or call it tags): test.el:

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.