Sorting is predetermining the complexity bound of O(N log(n)).
Suppose if a desired sub-array ends at i. Then we only have to look for no. of times a[i]+k and a[i]-k occurs before i. If the numbers are in range we can create a frequency array, else we can use a HashMap. Either way complexity will be O(n).
EDIT 1: I didn’t read the part less than equal to. Sorry MY bad
Also i was thinking if sorting is done can’t we use two pointer approach.
First in the sorted array low will represent a starting position and high will represent the number which is just less than or equal to a[low]+k.
All numbers in range [low,high] will make a pair with low.