All Tracks Data Structures Advanced Data Structures Fenwick (Binary Indexed) Trees Problem

Micro and Array Function
Tag(s):

Data Structures, Fenwick Tree, Medium

Problem
Editorial
Analytics

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\)

SAMPLE INPUT
1
3 2
1 2 3
SAMPLE OUTPUT
1
Explanation

\(X = [1]\), \(f(X,K) = 0\)
\(X = [2]\), \(f(X,K) = 0\)
\(X = [3]\), \(f(X,K) = 0\)
\(X = [1, 2]\), \(f(X,K) = 0\)
\(X = [2, 3]\), \(f(X,K) = 0\)
\(X = [1, 2, 3]\) , \(f(X,K) = 1\)

Time Limit: 2.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Swift-4.1, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

March Easy '17

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications

?