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

  • Home
  • SEARCH
  • 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 8372041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:18:58+00:00 2026-06-09T14:18:58+00:00

I know this question has been discussed before, but i am interested in doing

  • 0

I know this question has been discussed before, but i am interested in doing this using a Binary Indexed Tree. I found this link to show how to do it.I did not quite follow the explanation. Could someone please give me an explanation of why the following given there is true.

Create a BIT of size greater than n(no of elements). Iterate through array A (
let j be the index of loop),and for each element A[j] do:

1) Add j-sum(A[j]) to the number of inversions
2) add(A[j], 1) (i.e. add 1 to the position A[j] on BIT. This effectively 
counts the number of time value A[j] is seen so far)

I dont get why this works.

  • 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-09T14:19:00+00:00Added an answer on June 9, 2026 at 2:19 pm

    An inversion occurs when an element is larger than some element that follows it in the array.

    We can count inversions by grouping them by second element. For example, in the array [4, 3, 1, 2], the element pairs (4, 3), (4, 1), (4, 2), (3, 1), and (3, 2) are inversions. We group them by second element, hence: [[(4, 1), (3, 1)], [(4, 2), (3, 2)], [(4, 3)]].

    We consider each element in turn, and count how many inversions it is the second element of. In the example, the element 4 is the second element in 0 inversions, the element 3 in 1 inversion, and the elements 1 and 2 in 2 inversions each.

    In order for any given element to be the second element of an inversion, there has to be a larger element somewhere before it in the array.

    We perform the count efficiently by traversing the array from left to right and always keeping track of how many elements of each value have been encountered so far, using a BIT. Initially our frequency table will be [0, 0, 0, 0], since we’ve seen no elements at all. After we visit the 4, we update its frequency, giving [0, 0, 0, 1]. After visiting the 3, [0, 0, 1, 1], and so on.

    Each time we visit a position, we use the BIT to find out how many elements visited so far are greater than it. So for example when we encounter the 1, the BIT currently contains [0, 0, 1, 1], representing that there were so far zero 1’s and 2’s, one 3, and one 4. By adding the values 0 + 1 + 1, we count the number of elements so far that are greater than 1.

    Adding all these individual counts gives the total number of inversions.

    Note that, in general, you must employ coordinate compression in order for this to be efficient. For example, if your initial array contains numbers like A = [92, 631, 50, 7], you shouldn’t allocate a BIT with hundreds of elements. Instead, sort the array to determine that 7 < 50 < 92 < 631, which allows us to assign the ranks 7 => 1, 50 => 2, 92 => 3, 631 => 4; then replace each element by its rank, giving B = [3, 4, 2, 1]. The number of inversions of this array will be the same as in the original, since B[i] > B[j] if and only if A[i] > A[j].

    (Note: A real programmer would probably use indices starting from zero.)

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

Sidebar

Related Questions

I know this question has been posted before... but I haven't found any answer
I know this question has been discussed before, and I know this is such
I know this question has been asked before however the answers I found where
I know this question has been asked before, but non of the solutions solve
I know this question has been asked before, but I haven't been able to
I know this is a question that has been discussed over and over, but
I know this question has been asked before, but none of the previous answers
I know that my question has already been discussed on StackOverflow but i found
I know this question has been asked before but the solutions did not work
I know this question has been asked before, but I tried eveything and can't

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.