Subarray Inversions
Submissions: 358   Accuracy:

29.93%

  Difficulty: Medium   Marks: 4

Given an array A of n integers, find the sum of the number of inversions in all subarrays of length k. To clarify, determine the number of inversions in each of the n – k + 1 subarrays of length k and add them together.

Input:
The first line consists of an integer T i.e number of test cases. The first line of each test case contains two space separated integers n and k. The next line contains a sequence of n space separated integers.

Output:
Print the total number of subarray inversions.

Constraints: 
1<=T<=100
1<=n<=1000
1<=k 1<=a[i]<=100000

Example:
Input:

2
8 4
12 3 14 8 15 1 4 22
10 5
15 51 44 44 76 50 29 88 48 50

Output:
14
25

 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Saksham Ashtputre
Author: saksham0751


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.