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

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

hey,

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.

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!

hereās the solution : https://www.codechef.com/viewsolution/28559499