Minimize sum of points assigned to distinct elements present in an array
Given an array arr[] of size N, the task is to assign points to every distinct array element such that the sum of the points assigned is minimum possible.
The following conditions are required to be satisfied while assigning points:
- The minimum point that can be given to a number is 1.
- Every distinct element should be assigned a unique value.
- The points should be assigned in accordance with the value of the number, i.e. smaller numbers should be assigned lower points as compared to bigger numbers.
Examples:
Input: N = 5, arr[] = {10, 20, 10, 25}
Output: 7
Explanation: Assign 1 point to 10, 2 points to 20, 3 points to 25. Sum of points = 1 + 2 + 1 + 3 = 7, which is the minimum possible.
Input: N = 4, arr[] = {7, 7, 7, 7}
Output: 4
Explanation: Assign 1 point to 7. Sum of points = 1 + 1 + 1 + 1 = 4, which is the minimum possible.
Approach: The problem can be solved greedily by assigning lower points to smaller elements. Follow the steps below to solve the problem:
- Initialize a variable ans as 1 to store the required result.
- Sort the given array in increasing order.
- Initialize a variable point as 1 to store the points of the current element.
- Iterate in the range [0, N-1] using the variable i
- If arr[i] is equal to arr[i+1], then increment ans by point.
- Else increment the value of point by 1 and then add point to ans.
- Print the value of ans as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMinimumSum( int arr[], int n)
{
sort(arr, arr + n);
int point_value = 1;
int total_points = 1;
for ( int i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1])
total_points += point_value;
else {
point_value = point_value + 1;
total_points += point_value;
}
}
cout << total_points;
}
int main()
{
int arr[] = { 10, 20, 10, 25 };
int n = 4;
findMinimumSum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMinimumSum( int []arr, int n)
{
Arrays.sort(arr);
int point_value = 1 ;
int total_points = 1 ;
for ( int i = 0 ; i < n - 1 ; i++)
{
if (arr[i] == arr[i + 1 ])
total_points += point_value;
else
{
point_value = point_value + 1 ;
total_points += point_value;
}
}
System.out.print(total_points);
}
public static void main(String[] args)
{
int []arr = { 10 , 20 , 10 , 25 };
int n = 4 ;
findMinimumSum(arr, n);
}
}
|
Python3
def findMinimumSum(arr, n):
arr = sorted (arr)
point_value = 1
total_points = 1
for i in range (n - 1 ):
if (arr[i] = = arr[i + 1 ]):
total_points + = point_value
else :
point_value = point_value + 1
total_points + = point_value
print (total_points)
if __name__ = = '__main__' :
arr = [ 10 , 20 , 10 , 25 ]
n = 4
findMinimumSum(arr, n)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findMinimumSum( int []arr, int n)
{
Array.Sort(arr);
int point_value = 1;
int total_points = 1;
for ( int i = 0; i < n - 1; i++)
{
if (arr[i] == arr[i + 1])
total_points += point_value;
else
{
point_value = point_value + 1;
total_points += point_value;
}
}
Console.Write(total_points);
}
public static void Main()
{
int []arr = { 10, 20, 10, 25 };
int n = 4;
findMinimumSum(arr, n);
}
}
|
Javascript
<script>
function findMinimumSum(arr, n) {
arr.sort((a, b) => a - b);
let point_value = 1;
let total_points = 1;
for (let i = 0; i < n - 1; i++) {
if (arr[i] == arr[i + 1])
total_points += point_value;
else {
point_value = point_value + 1;
total_points += point_value;
}
}
document.write(total_points);
}
let arr = [10, 20, 10, 25];
let n = 4;
findMinimumSum(arr, n);
</script>
|
Time Complexity: O(N*logN)
Auxiliary Space: O(1)
Last Updated :
25 Jun, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...