📜  使数组成为前 N 个自然数的排列的最低成本

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

给定一个大小为N的正整数数组arr ,任务是找到使该数组成为前 N 个自然数排列的最小成本,其中将元素递增或递减 1 的成本为 1。
例子:

方法:

  1. 按升序对数组元素进行排序
  2. 遍历排序后的数组:
    • 检查第 i 个索引 (0 ≤ i < N)处的元素是否等于i + 1
    • 如果不是,则使其相等并保留两者之间的差异作为此操作的成本。
  3. 遍历完成后,打印执行操作的总成本。

下面是上述方法的实现:

C++
// C++ program to calculate minimum cost
// to make an Array a permutation
// of first N natural numbers
 
#include 
using namespace std;
 
// Function to calculate minimum cost
// for making permutation of size N
int make_permutation(int arr[], int n)
{
    // sorting the array in ascending order
    sort(arr, arr + n);
 
    // To store the required answer
    int ans = 0;
 
    // Traverse the whole array
    for (int i = 0; i < n; i++)
        ans += abs(i + 1 - arr[i]);
 
    // Return the required answer
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 5, 3, 8, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << make_permutation(arr, n);
}


Java
// Java program to calculate minimum cost
// to make an Array a permutation
// of first N natural numbers
import java.util.*;
 
class GFG{
  
// Function to calculate minimum cost
// for making permutation of size N
static int make_permutation(int arr[], int n)
{
    // sorting the array in ascending order
    Arrays.sort(arr);
  
    // To store the required answer
    int ans = 0;
  
    // Traverse the whole array
    for (int i = 0; i < n; i++)
        ans += Math.abs(i + 1 - arr[i]);
  
    // Return the required answer
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 5, 3, 8, 1, 1 };
    int n = arr.length;
  
    // Function call
    System.out.print(make_permutation(arr, n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to calculate minimum cost
# to make an Array a permutation
# of first N natural numbers
 
# Function to calculate minimum cost
# for making permutation of size N
def make_permutation(arr,  n) :
 
    # sorting the array in ascending order
    arr.sort();
 
    # To store the required answer
    ans = 0;
 
    # Traverse the whole array
    for i in range(n) :
        ans += abs(i + 1 - arr[i]);
 
    # Return the required answer
    return ans;
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 5, 3, 8, 1, 1 ];
    n = len(arr);
 
    # Function call
    print(make_permutation(arr, n));
 
    # This code is contributed by Yash_R


C#
// C# program to calculate minimum cost
// to make an Array a permutation
// of first N natural numbers
using System;
 
class GFG{
  
    // Function to calculate minimum cost
    // for making permutation of size N
    static int make_permutation(int []arr, int n)
    {
        // sorting the array in ascending order
        Array.Sort(arr);
      
        // To store the required answer
        int ans = 0;
      
        // Traverse the whole array
        for (int i = 0; i < n; i++)
            ans += Math.Abs(i + 1 - arr[i]);
      
        // Return the required answer
        return ans;
    }
      
    // Driver code
    public static void Main(string[] args)
    {
        int []arr = { 5, 3, 8, 1, 1 };
        int n = arr.Length;
      
        // Function call
        Console.WriteLine(make_permutation(arr, n));
    }
}
 
// This code is contributed by Yash_R


Javascript


输出:
5

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