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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:37:23+00:00 2026-05-20T03:37:23+00:00

Suppose I have a list of elements like [5, 3, 1, 2, 4] ,

  • 0

Suppose I have a list of elements like [5, 3, 1, 2, 4], and I want to compare two elements by position. Whichever comes first in the list is greater, or true. So:

compare(5, 3) # true
compare(2, 1) # false
compare(3, 4) # true

How can I do that in constant time? One way I thought of doing this was using maps, where the key is the element and the value is the position in the list:

order = {5: 0, 3: 1, 1: 2, 2: 3, 4: 4}

Then we have amortized O(1) time, but this will be O(N) space. Does anyone have a more elegant solution?

  • 1 1 Answer
  • 1 View
  • 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-20T03:37:24+00:00Added an answer on May 20, 2026 at 3:37 am

    Your map idea looks pretty good. The fact that a map is O(N) for memory shouldn’t be a problem, because you can’t get less than O(N) unless you use compression techniques (a list is O(N) as well).

    Also, since the map stores the indices of each element, you could forget about the original list, and just use the map. That is, unless you need the list for some reason. Even if you need to insert an element into the middle (say at position 3), you can update the map in linear time just by iterating over the element and incrementing the necessary indices.

    So, the map looks to be just as efficient a solution as the list for basic operations, with the added awesomeness of an O(1) compare function. As for elegance, the map is pretty hard to beat since it doens’t require any extra work beyond what’s described here.

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

Sidebar

Related Questions

Suppose I have a list with X elements [4,76,2,8,6,4,3,7,2,1...] I'd like the first 5
Suppose I have a List with the elements 4,7,9,17,24 and I want to insert
Suppose I have a huge list of elements with a structure like: <div class=item>
Let's suppose that I have a list of elements, and I want to select
Suppose I have a list like this <ul> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li>
Suppose I have an IEnumerable such as a List(TValue) and I want to keep
Suppose I have two lists that holds the list of source file names and
Suppose I have a bulleted list like this: * list item 1 * list
Suppose I have a simple Python list like this: >>> l=['0', '1', '2', '3',
Hi I am using Mathematica 5.2. Suppose I have an array list like In[2]:=lst=Tuples[{0,1},4]

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.