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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:45:20+00:00 2026-05-30T15:45:20+00:00

I had previously posted a question, Given an array, find out the next smaller

  • 0

I had previously posted a question, Given an array, find out the next smaller element for each element
now, i was trying to know , if there is any way to find out “given an array, for each element, find out the total number of elements lesser than it, which appear to the right of it”
for example, the array [4 2 1 5 3] should yield [3 1 0 1 0]??

[EDIT]
I have worked out a solution, please have a look at it, and let me know if there is any mistake.

1 Make a balanced BST inserting elements traversing the array from right to left

2 The BST is made in such a way that each element holds the size of the tree rooted at that element

3 Now while you search for the right position to insert any element, take account of the total size of the subtree rooted at left sibling + 1(for parent) if you move right
Now since, the count is being calculated at the time of insertion of an element, and that we are moving from right to left, we get the exact count of elements lesser than the given element appearing after it.

  • 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-30T15:45:22+00:00Added an answer on May 30, 2026 at 3:45 pm

    It can be solved in O(n log n).

    If in a BST you store the number of elements of the subtree rooted at that node when you search the node (reaching that from the root) you can count number of elements larger/smaller than that in the path:

    int count_larger(node *T, int key, int current_larger){
        if (*T == nil)
            return -1;
        if (T->key == key)
            return current_larger + (T->right_child->size);
        if (T->key > key)
            return count_larger(T->left_child, key, current_larger + (T->right_child->size) + 1);
        return count_larger(T->right_child, key, current_larger)
    }
    

    ** for example if this is our tree and we’re searching for key 3, count_larger will be called for:

    -> (node 2, 3, 0)
    –> (node 4, 3, 0)
    —> (node 3, 3, 2)

    and the final answer would be 2 as expected.

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

Sidebar

Related Questions

I'm having some frustrating alignment issues. I had previously posted a question because everything
I had previously posted a question about my query speed with an XML column.
Previously we had desktop applications but given the fact that accessing the server (either
I am trying to deploy a ClickOnce app. It had worked previously when the
I'm trying to get an uiimage from a calayer that I had previously rotated:
I just came out of a design meeting and had a question posed to
We had previously implemented tapping to zoom and now we've decided to use icons
This is a follow up to my related question posted previously. My .NET application
I previously posted a question on sharing resources across assemblies using Themes/generic.xaml and the
Somebody posted me this code to me with a problem I had: $('#txtWeight').each( function()

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.