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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:58:36+00:00 2026-06-18T06:58:36+00:00

Given an empty array, I need to make two type of queries Inserting an

  • 0

Given an empty array, I need to make two type of queries

  1. Inserting an element in the array

  2. Finding the index of some element k (obviously the array has to be kept sorted)

This can be done be using set container

set<int> st;
set.insert(t);

This will insert my element in O(log(n)).

And for 2nd query

set<int>::iterator it;
it = st.find(k);
idx = distance(st.begin(), it);

This takes O(n) time. (O(n) [for distance()[ + O(log(n) [for set::find()] ).

Is there any way to do both queries in O(log(n)) using the predefined containers of C++?

http://www.cplusplus.com/reference/stl/

  • 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-18T06:58:37+00:00Added an answer on June 18, 2026 at 6:58 am

    No. It is not possible (with the predefined containers). The sequence containers of the C++ Standard Library have either:

    • O(1) random access and O(N) insertion/removal
      or
    • O(N) random access and O(1) insertion/removal

    Note that deque is an exception, but only when the insertion/removal takes place at the ends of the array. The general case is still O(N).

    Furthermore, the classification of iterators does not include a category for this case. You have the bidirectional iterators (those of list, set, multiset, map and multimap), which take O(N) time to jump to a random position, and the next category is for random access iterators (those of vector, deque and string). There is no intermediate category.

    Adding a new category would not be trivial at all. The library also implements a lot of algorithms (like for_each) that work with containers. There is an implementation for every iterator category.

    Order statistic trees have been proposed at Boost several times. As far as I know:

    1. 2004: First suggestion (I don’t know if it was implemented)
    2. 2006: “Hierarchical Data Structures”
    3. 2006: AVL Array (renamed as “Rank List” in Boost)
    4. 2012: Counter tree

    The main difficulty for them being accepted was the generalized opinion that they were not a benefit, but a hazard. Today’s programmers are used to solve all the problems they know with the typical containers. Experienced programmers fear that newbies might blindly use the proposed container for everything, instead of choosing carefully.

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

Sidebar

Related Questions

I need to create a valid xml from a given array(); My Method looks
I need to define an array node with a given default value in a
I have been given an array in the following format and need to put
Given two object arrays I need to compare the differences between the two (when
I need to pass an empty Array of Variants to a DLL written in
Given an integer array, i need to find which number occurred most number of
I need one liner (or close to it) that verifies that given array of
Given the following simple BST definition: data Tree x = Empty | Leaf x
I need a certain part of a URL extracted. Example: http://www.domain.com/blog/entry-title/?standalone=1 is the given
Is there any Zend Helper that can make PDF document with tables. i need

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.