INVYCNT Editorial

Because these are the part of all possible inversions when array is concatenated.

Please do tell me what’s wrong in my solution.

you have to calculate p and q separately for every index, not overall.
have a look at my approach again, it’s mentioned to calculate for every index.

Bro I rectified my code. I ran all the possible test cases and matched my answer with the setter’s code and they were correct. Then I checked for the last test case. I took extreme cases and saw my answer didn’t match. Then I changed data type from int to long long int. Then my code got accepted. But thanks bud. I went with my approach only.

1 Like

ohh fine, I must have looked closely into your solution. Kudos!

My solution:

example : if N = 3 and A[] = {1, 2, 3} and K = 4 then,
we don’t need to make a repeated array of K times.
we will just assume K=2 and build the array of 2 * N size.
then array will be A = {1, 2, 3, 1 ,2, 3}
will will count if A[i] > A[j] and for every i we will take there count value
in an another array A_2.

then we will apply arithmetic series formulas toh find the Kth value of an element a_nth = a1 + (n-1) * d and then sum of series sum = ((a_1 + a_nth) * n) / 2 in array A_2. the total sum for every element in A_2 will be the answer!
Thanks! :slightly_smiling_face:

here’s the solution :