Input: arr[] = { 2, 3, 3}
Output: {2, 2, 3}
Explanation:
K = 1: {2, 2, 3}
There are two distinct element in above array = {2, 3}
when array is divided into 1 subarray
K = 2: {2, 2}, { 3 }
There is one type of element in first array and for second subarray,
So the sum of count of unique is 1+1 = 2.
The array can also be divided into 2 subarrays as follows {2, 3}, {2} but then
the count of unique elements will be 2 and 1 and the sum will be 2+1 = 3 which is not the minimum.
K = 3: {2}, {2}, {3}
Unique elements in first subarray = 1
Unique elements in second subarray = 1
Unique elements in third subarray = 1
So total capabilities = 1+1+1 = 3
Input: arr[] = { 3, 1, 2, 2, 2, 4}
Output: {4, 4, 4, 4, 5, 6}
Explanation:
K = 1: {1, 2, 2, 2, 4, 3}
There are 4 type of elements = { 1, 2, 3, 4}
So for k = 1, required sum is = 4
K = 2: {2, 2, 2, 4, 3}, {1}
There are 3 type of elements in first subarray = {2, 3, 4}
So unique elements in this subarray are 3
There is only one type of elements in second subarray = { 1}
So unique elements in this subarray is 1
So for k = 2, total minimum count = 3+1 = 4
K = 3: {2, 2, 2, 3}, {1}, {4}
For k = 3, total minimum count = 2+1+1 = 4
K = 4: {2, 2, 2}, {1}, {4}, {3}
For k = 4, total minimum count = 1+1+1+1 = 4
K = 5: {2, 2}, {1}, {2}, {4}, {3}
For k = 5, total minimum count = 2+1+1+1+1 = 5
K = 6: {1}, {2}, {2}, {2}, {4}, {3}
For k = 6, total minimum count = 1+1+1+1+1+1 = 6
Each subarray contains unique elements.
Refer the illustration below for a better understanding.
Consider array arr[] = {2, 2, 3}
Total number of unique elements = 2 (2 and 3)
For dividing it in k = 1 parts:
=> There cannot be less than 2 unique elements in total.
=> Divide it into subsets {2, 2, 3}.
=> Total count of unique elements are 2
For dividing it in k = 2 parts:
=> There cannot be less than 2 unique elements in total.
=> Divide it into subsets {2, 2} and {3}.
=> Total count of unique elements are 1 + 1 = 2
For dividing it in k = 3 parts:
=> To minimize total count of unique elements break only one group of similar elements and keep the others intact.
=> Here there was only one group with all similar elements: {2, 2}.
=> Break that into 2 parts: {2}, {2}.
=> Divide it into subsets {2}, {2} and {3}.
=> Total count of unique elements are 1 + 1 + 1 = 3
Below implementation of the above approach.