Minimize sum of distinct elements of all prefixes by rearranging Array
Given an array arr[] with size N, the task is to find the minimum possible sum of distinct elements over all the prefixes of the array that can be obtained by rearranging the elements of the array.
Examples:
Input: arr[] = {3, 3, 2, 2, 3}, N = 5
Output: 7
Explanation: The permutation arr[] = {3, 3, 3, 2, 2} gives the minimum cost. It can be proved that this is the least cost that can be obtained.Input: arr[] = {7, 2, 2, 4, 7, 1}, N = 6
Output: 13
Explanation: The permutation arr[] = {7, 7, 2, 2, 1, 4} gives the minimum cost. It can be proved that this is the least cost that can be obtained.
Approach: The main idea involved in the problem is:
Let’s denote the number of distinct elements encountered till the ith index by dist[i]. Then it can be proven that always dist[i + 1] ≥ dist[i]. The logic is simple- when we move from index i to next index i+1, we either come across a new element or an element that has already been met previously in the array.
Based on the above approach one can conclude that the smaller the index i, the smaller should be the value of dist[i] as dist[i+1] will be always greater than or equal to dist[i]. So while doing the computation of minimum cost one should minimize each value of dist[i] as much as possible. For this to happen, create a permutation of the array where the element with maximum frequency is placed first and all other elements are placed successively in decreasing order of their frequencies.
Illustration:
Consider: arr[] = {3, 3, 2, 2, 3}
The permutation of the above array that will give the minimum cost is [3, 3, 3, 2, 2]
The computation of cost for each of the prefixes of the above permutation is listed below:
- arr[1: 1], Number of distinct elements = 1
- arr[1: 2], Number of distinct elements = 1
- arr[1: 3], Number of distinct elements = 1
- arr[1: 4], Number of distinct elements = 2
- arr[1: 5], Number of distinct elements = 2
Hence, the Minimum cost for the given array will be 7.
Follow the below-mentioned steps to implement the approach :
- Count the number of occurrences of all the elements of the array and store them on a map.
- Store all the frequency values in an auxiliary array (say store[]) and sort the array in descending order.
- Evaluate the final result through the below logic :
- Each value in store[] indicates the frequency of some element in the main array.
- This means that while iterating from i to i+1 in store[], the number of different elements encountered increases by one each time.
- Hence, keep a counter and increment it for every iteration of the store[] array.
Below is the implementation of the above approach :
C++
|
Time Complexity: O(N2 * LogN)
Auxiliary space: O(N)
Related Articles:
Given an array arr[] with size N, the task is to find the minimum possible sum of distinct elements over all the prefixes of the array that can be obtained by rearranging the elements of the array.
Examples:
Input: arr[] = {3, 3, 2, 2, 3}, N = 5
Output: 7
Explanation: The permutation arr[] = {3, 3, 3, 2, 2} gives the minimum cost. It can be proved that this is the least cost that can be obtained.Input: arr[] = {7, 2, 2, 4, 7, 1}, N = 6
Output: 13
Explanation: The permutation arr[] = {7, 7, 2, 2, 1, 4} gives the minimum cost. It can be proved that this is the least cost that can be obtained.
Approach: The main idea involved in the problem is:
Let’s denote the number of distinct elements encountered till the ith index by dist[i]. Then it can be proven that always dist[i + 1] ≥ dist[i]. The logic is simple- when we move from index i to next index i+1, we either come across a new element or an element that has already been met previously in the array.
Based on the above approach one can conclude that the smaller the index i, the smaller should be the value of dist[i] as dist[i+1] will be always greater than or equal to dist[i]. So while doing the computation of minimum cost one should minimize each value of dist[i] as much as possible. For this to happen, create a permutation of the array where the element with maximum frequency is placed first and all other elements are placed successively in decreasing order of their frequencies.
Illustration:
Consider: arr[] = {3, 3, 2, 2, 3}
The permutation of the above array that will give the minimum cost is [3, 3, 3, 2, 2]
The computation of cost for each of the prefixes of the above permutation is listed below:
- arr[1: 1], Number of distinct elements = 1
- arr[1: 2], Number of distinct elements = 1
- arr[1: 3], Number of distinct elements = 1
- arr[1: 4], Number of distinct elements = 2
- arr[1: 5], Number of distinct elements = 2
Hence, the Minimum cost for the given array will be 7.
Follow the below-mentioned steps to implement the approach :
- Count the number of occurrences of all the elements of the array and store them on a map.
- Store all the frequency values in an auxiliary array (say store[]) and sort the array in descending order.
- Evaluate the final result through the below logic :
- Each value in store[] indicates the frequency of some element in the main array.
- This means that while iterating from i to i+1 in store[], the number of different elements encountered increases by one each time.
- Hence, keep a counter and increment it for every iteration of the store[] array.
Below is the implementation of the above approach :
C++
|
Time Complexity: O(N2 * LogN)
Auxiliary space: O(N)
Related Articles: