Minimum operations to change given array with at most 1 duplicate into a permutation of 1 to N
Last Updated :
11 Nov, 2021
Given an array arr[] having N integers in the range [1, N] with at most one repeated element, the task is to find the minimum number of increment or decrement operations required to make the given array a permutation of numbers from 1 to N.
Examples:
Input: arr[] = {1, 2, 5, 3, 2}
Output: 2
Explanation: The given array contains 2 twice and 4 is missing from the array. Therefore, a 2 can be changed into 4 using two increment operations which is the minimum possible.
Input: arr[] = {1, 2, 5, 3, 4}
Output: 0
Explanation: The given array already represents the permutation of integers from 1 to 5.
Naive Approach: The given problem can be solved simply by finding the duplicate element and the missing element in the given array. This can be easily done by sorting the given array and checking for the duplicate and the missing element.
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Efficient Approach: The given problem can be solved using a simple observation that the sum of all elements of a permutation of N integers is always equal to N*(N+1)/ 2. Hence, the number of required operations can simply be calculated by the formula | sum of array elements – N*(N+1)/ 2)|.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
int minCost( int arr[], int N)
{
int sumOfArray = 0;
for ( int i = 0; i < N; i++) {
sumOfArray += arr[i];
}
int sumOfN = N * (N + 1) / 2;
int diff = sumOfArray - sumOfN;
diff = diff < 0 ? -1 * diff : diff;
return diff;
}
int main()
{
int arr[] = { 1, 2, 5, 3, 2 };
int n = sizeof (arr) / sizeof ( int );
cout << minCost(arr, n);
return 0;
}
|
Java
public class GFG {
static int minCost( int []arr, int N)
{
int sumOfArray = 0 ;
for ( int i = 0 ; i < N; i++) {
sumOfArray += arr[i];
}
int sumOfN = N * (N + 1 ) / 2 ;
int diff = sumOfArray - sumOfN;
diff = diff < 0 ? - 1 * diff : diff;
return diff;
}
public static void main (String[] args) {
int arr[] = { 1 , 2 , 5 , 3 , 2 };
int n = arr.length;
System.out.println(minCost(arr, n));
}
}
|
Python3
def minCost(arr, N):
sumOfArray = 0
for i in range ( 0 , N):
sumOfArray + = arr[i]
sumOfN = N * (N + 1 ) / / 2
diff = sumOfArray - sumOfN
if diff < 0 :
diff = - 1 * diff
return diff
if __name__ = = "__main__" :
arr = [ 1 , 2 , 5 , 3 , 2 ]
n = len (arr)
print (minCost(arr, n))
|
C#
using System;
class GFG {
static int minCost( int []arr, int N)
{
int sumOfArray = 0;
for ( int i = 0; i < N; i++) {
sumOfArray += arr[i];
}
int sumOfN = N * (N + 1) / 2;
int diff = sumOfArray - sumOfN;
diff = diff < 0 ? -1 * diff : diff;
return diff;
}
public static void Main () {
int []arr = { 1, 2, 5, 3, 2 };
int n = arr.Length;
Console.Write(minCost(arr, n));
}
}
|
Javascript
<script>
function minCost(arr, N)
{
let sumOfArray = 0;
for (let i = 0; i < N; i++) {
sumOfArray += arr[i];
}
let sumOfN = N * (N + 1) / 2;
let diff = sumOfArray - sumOfN;
diff = diff < 0 ? -1 * diff : diff;
return diff;
}
let arr = [1, 2, 5, 3, 2];
let n = arr.length;
document.write(minCost(arr, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...