Given an array, we have to calculate how many subarrays whose absolute difference of first and last element is less than or equal to K.

My approach :

```
lli n;
cin>>n;
lli a[n];
scanarr(a,n);
lli k;
cin>>k;
sort(a,a+n);
lli count = 0;
for(lli low = 0, high = 0 ; high<n;high++)
{
for(;abs(a[high]-a[low])>k ; low++);
count += (high-low+1);
}
print(count);
```

This code is optimized but in worst case it is O(N^{2})

similar question: “https://www.geeksforgeeks.org/pairs-difference-less-k/”

This question is also asked in CN contest, I thought this approach will not work as Array size is long but it works.

Help @galencolin @carre @aneee004