The Wayback Machine - https://web.archive.org/web/20210615033244/https://www.geeksforgeeks.org/minimize-sum-of-numbers-required-to-convert-an-array-into-a-permutation-of-first-n-natural-numbers/
Skip to content
Related Articles

Related Articles

Improve Article
Minimize sum of numbers required to convert an array into a permutation of first N natural numbers
  • Last Updated : 14 Jun, 2021

Given an array A[] of size N, the task is to find the minimum sum of numbers required to be added to array elements to convert the array into a permutation of 1 to N. If the array can not be converted to desired permutation, print -1.

Examples:

Input: A[] = {1, 1, 1, 1, 1}
Output: 10
Explanation: Increment A[1] by 1, A[2] by 2, A[3] by 3, A[4] by 4, thus A[] becomes {1, 2, 3, 4, 5}.
Minimum additions required = 1 + 2 + 3 + 4 = 10

Input: A[] = {2, 2, 3}
Output: -1

Approach: The idea is to use sorting. Follow these steps to solve this problem:



Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to find the minimum additions required 
// to convert the array into a permutation of 1 to N
int minimumAdditions(int a[], int n)
{
    // Sort the array in increasing order
    sort(a, a + n);
    int ans = 0;
 
    // Traverse the array
    for (int i = 0; i < n; i++) {
 
        // If a[i] > i + 1, then return -1
        if ((i + 1) - a[i] < 0) {
            return -1;
        }
        if ((i + 1) - a[i] > 0) {
 
            // Update answer
            ans += (i + 1 - a[i]);
        }
    }
 
    // Return the required result
    return ans;
}
 
// Driver Code
int main()
{
    // Given Input
    int A[] = { 1, 1, 1, 1, 1 };
    int n = sizeof(A) / sizeof(A[0]);
 
    // Function Call
    cout << minimumAdditions(A, n);
 
    return 0;
}

Python3




# Python3 program for the above approach
 
# Function to find the minimum additions
# required to convert the array into a
# permutation of 1 to N
def minimumAdditions(a, n):
     
    # Sort the array in increasing order
    a = sorted(a)
    ans = 0
 
    # Traverse the array
    for i in range(n):
 
        # If a[i] > i + 1, then return -1
        if ((i + 1) - a[i] < 0):
            return -1
 
        if ((i + 1) - a[i] > 0):
 
            # Update answer
            ans += (i + 1 - a[i])
 
    # Return the required result
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    # Given Input
    A = [ 1, 1, 1, 1, 1 ]
    n = len(A)
 
    # Function Call
    print(minimumAdditions(A, n))
 
# This code is contributed by mohit kumar 29
Output: 
10

 

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

 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 




My Personal Notes arrow_drop_up
Recommended Articles
Page :