Input: M = 3, arr[] = {1, 1, 1, 1, 2, 3}
Output: 2
Explanation:
Replace the subarray over the range [2, 3] with the element {2, 3} modifies the array arr[] to {1, 1, 2, 3, 2, 3}. Now, the frequency of each array elements is N / M( = 6 / 3 = 2).
Therefore, the minimum length subarray that needed to be replaced is 2.
Input: M = 6, arr[] = {1, 3, 6, 6, 2, 1, 5, 4, 1, 4, 1, 2, 3, 2, 2, 2, 4, 3}
Output: 4
Below is the implementation of the above approach.