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
// C++ program of the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript Program to implement
// the above approach
// Function to find the minimum number of
// increment/decrement operations to change
// the given array into a permutation
function minCost(arr, N)
{
// Stores the sum of array elements
let sumOfArray = 0;
// Loop to iterate through the array
for (let i = 0; i < N; i++) {
sumOfArray += arr[i];
}
// Finding sum of first
// N natural numbers
let sumOfN = N * (N + 1) / 2;
// Find the absolute difference
let diff = sumOfArray - sumOfN;
diff = diff < 0 ? -1 * diff : diff;
// Return Answer
return diff;
}
// Driver code
let arr = [1, 2, 5, 3, 2];
let n = arr.length;
document.write(minCost(arr, n));
// This code is contributed by Potta Lokesh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize modulo operations to make given Array a permutation of [1, N] Given an array arr[] of size N, the task is to find the minimum number of operations required to make the array a permutation of numbers in range [1, N] where, in each operation, an element at any index i can be replaced by arr[i]%k (k = any value greater than 0). Return -1 if the array cannot be ma
7 min read
Minimum operations to convert an Array into a Permutation of 1 to N by replacing with remainder from some d Given an array arr[] of size N, the task is to find the minimum number of operations to convert the array into a permutation of [1, n], in each operation, an element a[i] can be replaced by a[i] % d where d can be different in each operation performed. If it is not possible print -1. Examples: Input
8 min read
Check if given permutation of 1 to N is feasible using given operations Given an array arr[] of size N, the task to check if this array is built under following constraints: The array can only contain numbers from 1 to N.We have to build the array sequentially. It means at first we place 1 then 2 and so on upto N.If the array is empty then we can place a number at any p
9 min read
Find all duplicate and missing numbers in given permutation array of 1 to N Given an array arr[] of size N consisting of the first N natural numbers, the task is to find all the repeating and missing numbers over the range [1, N] in the given array. Examples: Input: arr[] = {1, 1, 2, 3, 3, 5}Output: Missing Numbers: [4, 6]Duplicate Numbers: [1, 3]Explanation:As 4 and 6 are
8 min read
Minimum steps to convert an Array into permutation of numbers from 1 to N Given an array arr of length N, the task is to count the minimum number of operations to convert given sequence into a permutation of first N natural numbers (1, 2, ...., N). In each operation, increment or decrement an element by one.Examples: Input: arr[] = {4, 1, 3, 6, 5} Output: 4 Apply decremen
4 min read
Change the array into a permutation of numbers from 1 to n Given an array A of n elements. We need to change the array into a permutation of numbers from 1 to n using minimum replacements in the array. Examples: Input : A[] = {2, 2, 3, 3} Output : 2 1 3 4 Explanation: To make it a permutation of 1 to 4, 1 and 4 are missing from the array. So replace 2, 3 wi
5 min read
Minimize cost to make given Array a permutation of 1 to N by given replacements Given two arrays a[] and b[] of length N and an integer K (1 ⤠K ⤠N). All the integers in array a[] lie within the range [1, K]. ith value in array b[] denotes the cost of replacing a[i] with any number in the range [1, N]. The task is to find the minimum cost to replace the elements of array a[] t
6 min read
Minimum count of elements required to obtain the given Array by repeated mirror operations Given an array arr[] consisting of N integers, the task is to find the array K[] of minimum possible length such that after performing multiple mirror operations on K[], the given array arr[] can be obtained. Mirror Operation: Appending all the array elements to the original array in reverse order.
7 min read
Minimize operations to make Array a permutation Given an array arr of n integers. You want to make this array a permutation of integers 1 to n. In one operation you can choose two integers i (0 ⤠i < n) and x (x > 0), then replace arr[i] with arr[i] mod x, the task is to determine the minimum number of operations to achieve this goal otherw
7 min read
Minimum p[i] = p[arr[i]] operations to regain the given Array Given an array, A[] (1 - indexed) of size 'N' which contains a permutation of [1, N], the task is to find the minimum number of operations to be applied on any array P[] to get back the original array P[]. The operation must be applied at least once. In each operation, for every index of P[] we set
12 min read