Split array into minimum number of subsets having difference between maximum and minimum element at most K

Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number of sets, the array elements can be divided into such that the difference between the maximum and minimum element of each set is at most K.Examples:Input: arr[] = {1, 2, 3, 4, 5}, K = 2 Output: 2Explanation:The given array can be divided into two sets {1, 2, 3} having the difference between maximum and minimum as 3 – 1= 2 and {4, 5} having the difference between maximum and minimum as 5 – 4 = 1.Input: arr[] = {5, 2, 9, 7, 3, 2, 4, 6, 14, 10}, K = 3Output: 4Approach: The given problem can be solved by sorting the given array and finding the minimum number of subarrays the array elements can be broken such that the difference between the maximum and minimum element at most K. Follow the steps below to solve the given problm:Sort the given array arr[] in non-decreasing order.Initialize two iterators begin and end as 0 representing the beginning and end of each set.Initialize a variable, say setCount as 1 that stores the resultant minimum number of breaking of array elements into subarrays.Iterate a loop until the value of end is less than N and perform the following steps:If the value of arr[end] – arr[begin]