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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:34:57+00:00 2026-06-12T02:34:57+00:00

This is a question from a programming competition ( which has ended ). I

  • 0

This is a question from a programming competition ( which has ended ). I was struggling to solve this problem but couldn’t find a healthy method to do so.

The question is as follows:

IIIT Allahabad is celebrating its annual Techno-Cultural Fiesta Effervescence MM12 from 1st to 5th October. The Chef has agreed to supply candies for this festive season. The chef has prepared N boxes of candies, numbered 1 to N (Each number occurring exactly once ). The Chef is very particular about the arrangement of boxes. He wants boxes to be arranged in a particular order, but unfortunately Chef is busy. He has asked you to rearrange the boxes for him. Given the current order of boxes, you have to rearrange the boxes in the specified order. However there is a restriction.You can only swap two adjacent boxes to achieve the required order. Output, the minimum number of such adjacent swaps required.

Input

First line of input contains a single integer T, number of test cases. Each test case contains 3 lines, first line contains a single integer N, number of boxes. Next 2 lines contains N numbers each, first row is the given order of boxes while second row is the required order.

Output

For each test case, output a single integer ‘K’, minimum number of adjacent swaps required.
Constraints:

1<=T<=10
1<=N<=10^5

Example

Input:

4

3
1 2 3
3 1 2

3
1 2 3
3 2 1

5
3 4 5 2 1  
4 1 5 2 3  

4
1 2 3 4
2 3 4 1

Output:

2
3
6
3

I was almost clueless about this question. Can somebody please explain the logic behind the question!!

  • 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-12T02:34:58+00:00Added an answer on June 12, 2026 at 2:34 am

    The problem is quite a “classic” competitive programming problem, which is counting inversions in an array. Inversion is defined as a pair (i, j) where i < j and A[i] > A[j].

    The most general version, which an array of arbitrary numbers are given and you are asked to count the number of inversions, has an O(n log n) solution by modifying merge sort algorithm.

    A more restricted version^, in which there is a reasonable upperbound on the maximum value in the array (note that this is NOT the length of the array), can be solved in O(n log m), where m is the maximum value in the array. The main point here is the amount of code you have to write is much less than merge sort approach.

    The problem in the question is about counting the number of swaps to sort the array to certain order, which can be reconstructed as counting the number of swaps to sort the array in ascending order, and it boils down to count the number of inversions. Why number of inversions? Because you can only resolve at most one inversion per swapping 2 adjacent elements.

    You need to create an array that describes the current position of the boxes with respect to the final settings. Then the algorithm can start:

    1. Construct a Fenwick Tree (Binary Indexed Tree) with length m (m = n for the problem in the question).

      We will use the Fenwick Tree to help us in counting the number of preceding elements in the array that is larger than the current element. We will keep the frequency of the numbers that we have encountered so far and use Fenwick Tree range sum query to get the number of elements less than current element (and derive the number of elements larger than current element).

    2. Loop through n elements of the array:

      • Use range sum query to count how many numbers that is smaller than the current number has been recorded.
      • Use the info above to find out how many numbers that is larger than the current number. Add this to the inversion count. Take note to not include the element being considered. (*)
      • Adjust + 1 to the Fenwick Tree at the value of the element.
    3. The inversion count that is accumulated in the (*) step.

    ^ The question clearly says the elements are unique, so the algorithm above will work. I’m just not sure whether uniqueness is necessary condition, or the algorithm can be modified to accommodate the case where there are repeating elements.

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

Sidebar

Related Questions

This is very basic question from programming point of view but as I am
This is a question from some programming contest( i don't know exactly which programming
i have question from programming pearls problem is following show how to use Lomuto's
This is a question from the most recent version of Stroustrup's The C++ Programming
(This question is not about programming, but about how to avoid doing any programming.
I'm learning the basics of programming here (C#) but I think this question is
I'm doing a programming question from C++ Primer Plus which asks me to make
Different from this question , but similar in that I don't get an error
This is not a programming question, but a question on the best practice for
I know this question has been asked before, but this was one and a

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.