# Minimize sum of points assigned to distinct elements present in an array

• Last Updated : 25 Jun, 2021

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++

 `// C++ program for the above approach``#include ``using` `namespace` `std;` `// Function to assign points to each``// number in an array such that the``// sum of points assigned is minimized``void` `findMinimumSum(``int` `arr[], ``int` `n)``{``    ``// Sort the array in increasing order``    ``sort(arr, arr + n);` `    ``// Assign point_value to 1``    ``int` `point_value = 1;` `    ``// Store the required result``    ``int` `total_points = 1;` `    ``// Traverse the array, arr[]``    ``for` `(``int` `i = 0; i < n - 1; i++) {` `        ``// If adjacent elements are same add``        ``// current point_value to total_point``        ``if` `(arr[i] == arr[i + 1])``            ``total_points += point_value;` `        ``// Otherwise, assign a new point_value``        ``// and add it to total_points``        ``else` `{``            ``point_value = point_value + 1;``            ``total_points += point_value;``        ``}``    ``}` `    ``// Print the result``    ``cout << total_points;``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `arr[] = { 10, 20, 10, 25 };``    ``int` `n = 4;` `    ``// Function Call``    ``findMinimumSum(arr, n);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;``class` `GFG{``    ` `// Function to assign points to each``// number in an array such that the``// sum of points assigned is minimized``static` `void` `findMinimumSum(``int` `[]arr, ``int` `n)``{``    ` `    ``// Sort the array in increasing order``    ``Arrays.sort(arr);` `    ``// Assign point_value to 1``    ``int` `point_value = ``1``;` `    ``// Store the required result``    ``int` `total_points = ``1``;` `    ``// Traverse the array, arr[]``    ``for``(``int` `i = ``0``; i < n - ``1``; i++)``    ``{``        ` `        ``// If adjacent elements are same add``        ``// current point_value to total_point``        ``if` `(arr[i] == arr[i + ``1``])``            ``total_points += point_value;` `        ``// Otherwise, assign a new point_value``        ``// and add it to total_points``        ``else``        ``{``            ``point_value = point_value + ``1``;``            ``total_points += point_value;``        ``}``    ``}` `    ``// Print the result``    ``System.out.print(total_points);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``// Given Input``    ``int` `[]arr = { ``10``, ``20``, ``10``, ``25` `};``    ``int` `n = ``4``;` `    ``// Function Call``    ``findMinimumSum(arr, n);``}``}` `// This code is contributed by sanjoy_62.`

## Python3

 `# Python3 program for the above approach` `# Function to assign points to each``# number in an array such that the``# sum of points assigned is minimized``def` `findMinimumSum(arr, n):``    ` `    ``# Sort the array in increasing order``    ``arr ``=` `sorted``(arr)` `    ``# Assign point_value to 1``    ``point_value ``=` `1` `    ``# Store the required result``    ``total_points ``=` `1` `    ``# Traverse the array, arr[]``    ``for` `i ``in` `range``(n ``-` `1``):` `        ``# If adjacent elements are same add``        ``# current point_value to total_point``        ``if` `(arr[i] ``=``=` `arr[i ``+` `1``]):``            ``total_points ``+``=` `point_value` `        ``# Otherwise, assign a new point_value``        ``# and add it to total_points``        ``else``:``            ``point_value ``=` `point_value ``+` `1``            ``total_points ``+``=` `point_value` `    ``# Print the result``    ``print``(total_points)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given Input``    ``arr ``=` `[ ``10``, ``20``, ``10``, ``25` `]``    ``n ``=` `4` `    ``# Function Call``    ``findMinimumSum(arr, n)` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to assign points to each``// number in an array such that the``// sum of points assigned is minimized``static` `void` `findMinimumSum(``int` `[]arr, ``int` `n)``{``    ` `    ``// Sort the array in increasing order``    ``Array.Sort(arr);` `    ``// Assign point_value to 1``    ``int` `point_value = 1;` `    ``// Store the required result``    ``int` `total_points = 1;` `    ``// Traverse the array, arr[]``    ``for``(``int` `i = 0; i < n - 1; i++)``    ``{``        ` `        ``// If adjacent elements are same add``        ``// current point_value to total_point``        ``if` `(arr[i] == arr[i + 1])``            ``total_points += point_value;` `        ``// Otherwise, assign a new point_value``        ``// and add it to total_points``        ``else``        ``{``            ``point_value = point_value + 1;``            ``total_points += point_value;``        ``}``    ``}` `    ``// Print the result``    ``Console.Write(total_points);``}` `// Driver Code``public` `static` `void` `Main()``{``    ` `    ``// Given Input``    ``int` `[]arr = { 10, 20, 10, 25 };``    ``int` `n = 4;` `    ``// Function Call``    ``findMinimumSum(arr, n);``}``}` `// This code is contributed by SURENDRA_GANGWAR`

## Javascript

 ``

Output:

`7`

Time Complexity: O(N*logN)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up