Sponsored
Sponsored
Use these hints if you're stuck. Try solving on your own first.
Use a sliding window approach keeping the maximum and minimum value using a data structure like a multiset from STL in C++.
More specifically, use the two pointer technique, moving the right pointer as far as possible to the right until the subarray is not valid (maxValue - minValue > limit), then moving the left pointer until the subarray is valid again (maxValue - minValue <= limit). Keep repeating this process.