📜  最小化将数组转换为前 N 个自然数的排列所需的数字总和

📅  最后修改于: 2021-10-26 06:04:52             🧑  作者: Mango

给定大小为N的数组A[] ,任务是找到将数组转换为1N的排列所需添加到数组元素的最小数字总和。如果数组无法转换为所需的排列,则打印-1

例子:

方法:想法是使用排序。请按照以下步骤解决此问题:

  • 初始化一个变量ans来存储所需的结果。
  • 按升序对给定数组A[]进行排序。
  • 使用变量i遍历数组A[]
    • 如果A[i] > i+1的值,ans更新为-1并跳出循环。
    • 否则通过i+1A[i]的差异增加ans
  • 打印ans的值作为结果。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
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;
}


Java
// Java program for the above approach
import java.util.Arrays;
 
public class GFG {
 
    // Function to find the minimum additions required 
    // to convert the array into a permutation of 1 to N
    static int minimumAdditions(int a[], int n)
    {
        // Sort the array in increasing order
        Arrays.sort(a);
        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
    public static void main(String[] args)
    {
       
      // Given Input
        int A[] = { 1, 1, 1, 1, 1 };
        int n = A.length;
 
        // Function Call
        System.out.println(minimumAdditions(A, n));
    }
}
 
// This code is contributed by abhinavjain194


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


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the minimum additions
// required to convert the array into a
// permutation of 1 to N
static int minimumAdditions(int []a, int n)
{
     
    // Sort the array in increasing order
    Array.Sort(a);
    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
static void Main()
{
     
    // Given Input
    int[] A = { 1, 1, 1, 1, 1 };
    int n = A.Length;
     
    // Function Call
    Console.Write(minimumAdditions(A, n));
}
}
 
// This code is contributed by SoumikMondal


Javascript


输出:
10

时间复杂度: O(N* log(N))
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程