Micro and Array Function
Practice
4.7 (3 votes)
Approved
Data structures
Fenwick tree
Fenwick tree
Medium
Problem
39% Success 1028 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Micro has made a breakthrough. He made a function which he proudly calls Micro's Array Fucntion. It takes an array of integers A and an integer K as input and optimally finds out the number of unordered pairs \((i, j), i \neq j\) such that \(|A[i]-A[j]| \ge K\).
Micro is getting a lot of recognition because of it. His best friend Mike wants to be a part of it, but for that he needs to make some contribution. He thought of extending Micro's Array function. He wants to make a new function \(g(A,K)\) that also takes an array of integers A and an integer K as input and optimally calculates \( \sum{f(X, K)} \) for all contiguous subarrays X of A. He need your help in this and help here means do the entire task. He'll give you an integer K and an array A having N integers and you need to compute \(g(A,K)\).

Input:
First line consists of a single integers T denoting the number of test cases.
First line of each test case consists of two space separated integer denoting N and K.
Second line of each test case consists of a N space separated integers denoting the array A.

Output:
For each test case, print the value of \(g(A,K)\) in a new line.

Constraints:
\(1 \le T \le 5\)
\(1 \le N \le 10^5\)
\(1 \le A[i], K \le 10^9\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
4 votes
Tags:
Data StructuresFenwick treeMedium
Points:30
3 votes
Tags:
ApprovedData StructuresFenwick treeMediumOpenSegment TreesTrees
Points:30
29 votes
Tags:
AlgorithmsApprovedImplementationMediumOpenTrees