给定大小为N的正整数的数组arr ,任务是找到使该数组成为前N个自然数的排列的最小开销,其中将元素递增或递减1的开销为1。
例子:
Input: arr[] = {1, 1, 7, 4}
Output: 5
Explanation:
Perform increment operation on 1 one time
Perform decrement operation on 7 four times
Resultant array = {1, 2, 3, 4}
Input: arr[] = {1, 2, 3, 4, 5}
Output: 0
Explanation:
The array is already a permutation.
方法:
- 按升序对数组元素进行排序
- 遍历排序后的数组:
- 检查第i个索引(0≤i
的元素是否等于i +1 。 - 如果不是,则使其相等,并且将两者之间的差额视为该操作的成本。
- 检查第i个索引(0≤i
- 遍历完成后,打印所执行操作的总成本。
下面是上述方法的实现:
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
输出:
5