📌  相关文章
📜  将数组转换为从1到N的数字排列的最少步骤

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

给定长度为N的数组arr ,任务是计算将给定序列转换为前N个自然数(1、2,…,N)的排列的最小操作数。在每个操作中,将元素递增或递减一个。
例子:

方法:一种有效的方法是对给定的数组进行排序,并为每个元素找到arr [i]i (基于1的索引)之间的差异。找到所有这些差异的总和,这将是所需的最少步骤。
下面是上述方法的实现:

CPP
// C++ program to find minimum number of steps to
// convert a given sequence into a permutation
 
#include 
using namespace std;
 
// Function to find minimum number of steps to
// convert a given sequence into a permutation
int get_permutation(int arr[], int n)
{
    // Sort the given array
    sort(arr, arr + n);
 
    // To store the required minimum
    // number of operations
    int result = 0;
 
    // Find the operations on each step
    for (int i = 0; i < n; i++) {
        result += abs(arr[i] - (i + 1));
    }
 
    // Return the answer
    return result;
}
 
// Driver code
int main()
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << get_permutation(arr, n);
 
    return 0;
}


Java
// Java program to find minimum number of steps to
// convert a given sequence into a permutation
import java.util.*;
 
class GFG{
  
// Function to find minimum number of steps to
// convert a given sequence into a permutation
static int get_permutation(int arr[], int n)
{
    // Sort the given array
    Arrays.sort(arr);
  
    // To store the required minimum
    // number of operations
    int result = 0;
  
    // Find the operations on each step
    for (int i = 0; i < n; i++) {
        result += Math.abs(arr[i] - (i + 1));
    }
  
    // Return the answer
    return result;
}
  
// Driver code
public static void main(String[] args)
{
    int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 };
  
    int n = arr.length;
  
    // Function call
    System.out.print(get_permutation(arr, n));
  
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find minimum number of steps to
# convert a given sequence into a permutation
 
# Function to find minimum number of steps to
# convert a given sequence into a permutation
def get_permutation(arr, n):
 
    # Sort the given array
    arr = sorted(arr)
 
    # To store the required minimum
    # number of operations
    result = 0
 
    # Find the operations on each step
    for i in range(n):
        result += abs(arr[i] - (i + 1))
 
    # Return the answer
    return result
 
# Driver code
if __name__ == '__main__':
    arr=[0, 2, 3, 4, 1, 6, 8, 9]
    n = len(arr)
 
    # Function call
    print(get_permutation(arr, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find minimum number of steps to
// convert a given sequence into a permutation
using System;
  
class GFG{
 
// Function to find minimum number of steps to
// convert a given sequence into a permutation
static int get_permutation(int []arr, int n)
{
    // Sort the given array
    Array.Sort(arr);
 
    // To store the required minimum
    // number of operations
    int result = 0;
 
    // Find the operations on each step
    for (int i = 0; i < n; i++) {
        result += Math.Abs(arr[i] - (i + 1));
    }
 
    // Return the answer
    return result;
}
 
// Driver Code
public static void Main()
{
    int []arr = { 0, 2, 3, 4, 1, 6, 8, 9 };
 
    int n = arr.Length;
 
    // Function call
    Console.Write(get_permutation(arr, n));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
7