📌  相关文章
📜  将最多具有 1 个重复的给定数组更改为 1 到 N 的排列的最小操作

📅  最后修改于: 2022-05-13 01:56:05.374000             🧑  作者: Mango

将最多具有 1 个重复的给定数组更改为 1 到 N 的排列的最小操作

给定一个数组arr[] ,在[1, N]范围内有N个整数,最多有一个重复元素,任务是找到使给定数组成为从1到数字的排列所需的最小递增或递减操作数ñ

例子:

朴素方法:给定问题可以简单地通过在给定数组中找到重复元素和缺失元素来解决。这可以通过对给定数组进行排序并检查重复元素和缺失元素来轻松完成。

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

有效方法:给定的问题可以通过一个简单的观察来解决,即N个整数的排列的所有元素的总和始终等于N*(N+1)/ 2 。因此,所需操作的数量可以简单地通过公式 |数组元素之和 – N*(N+1)/ 2)| .

下面是上述方法的实现:

CPP
// C++ program of the above approach
#include 
using namespace std;
 
// Function to find the minimum number of
// increment/decrement operations to change
// the given array into a permutation
int minCost(int arr[], int N)
{
    // Stores the sum of array elements
    int sumOfArray = 0;
 
    // Loop to iterate through the array
    for (int i = 0; i < N; i++) {
        sumOfArray += arr[i];
    }
 
    // Finding sum of first
    // N natural numbers
    int sumOfN = N * (N + 1) / 2;
 
    // Find the absolute difference
    int diff = sumOfArray - sumOfN;
    diff = diff < 0 ? -1 * diff : diff;
 
    // Return Answer
    return diff;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 2, 5, 3, 2 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << minCost(arr, n);
 
    return 0;
}


Java
// Java program of the above approach
 
public class GFG {
     
    // Function to find the minimum number of
    // increment/decrement operations to change
    // the given array into a permutation
    static int minCost(int []arr, int N)
    {
        // Stores the sum of array elements
        int sumOfArray = 0;
     
        // Loop to iterate through the array
        for (int i = 0; i < N; i++) {
            sumOfArray += arr[i];
        }
     
        // Finding sum of first
        // N natural numbers
        int sumOfN = N * (N + 1) / 2;
     
        // Find the absolute difference
        int diff = sumOfArray - sumOfN;
        diff = diff < 0 ? -1 * diff : diff;
     
        // Return Answer
        return diff;
    }
     
    // Driver code
    public static void main (String[] args) {
         
        int arr[] = { 1, 2, 5, 3, 2 };
        int n = arr.length;
     
        System.out.println(minCost(arr, n));
    }
}
 
// This code is contributed by AnkThon


Python3
# python program of the above approach
 
# Function to find the minimum number of
# increment/decrement operations to change
# the given array into a permutation
def minCost(arr, N):
 
    # Stores the sum of array elements
    sumOfArray = 0
 
    # Loop to iterate through the array
    for i in range(0, N):
        sumOfArray += arr[i]
 
        # Finding sum of first
        # N natural numbers
    sumOfN = N * (N + 1) // 2
 
    # Find the absolute difference
    diff = sumOfArray - sumOfN
    if diff < 0:
        diff = -1 * diff
 
        # Return Answer
    return diff
 
# Driver code
if __name__ == "__main__":
 
    arr = [1, 2, 5, 3, 2]
    n = len(arr)
 
    print(minCost(arr, n))
 
    # This code is contributed by rakeshsahni


C#
// C# program of the above approach
using System;
class GFG {
     
    // Function to find the minimum number of
    // increment/decrement operations to change
    // the given array into a permutation
    static int minCost(int []arr, int N)
    {
        // Stores the sum of array elements
        int sumOfArray = 0;
     
        // Loop to iterate through the array
        for (int i = 0; i < N; i++) {
            sumOfArray += arr[i];
        }
     
        // Finding sum of first
        // N natural numbers
        int sumOfN = N * (N + 1) / 2;
     
        // Find the absolute difference
        int diff = sumOfArray - sumOfN;
        diff = diff < 0 ? -1 * diff : diff;
     
        // Return Answer
        return diff;
    }
     
    // Driver code
    public static void Main () {
         
        int []arr = { 1, 2, 5, 3, 2 };
        int n = arr.Length;
     
        Console.Write(minCost(arr, n));
    }
}
// This code is contributed by Samim Hossain Mondal


Javascript


输出
2

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