Minimum De-arrangements present in array of AP (Arithmetic Progression)
Last Updated :
13 Jun, 2022
Given an array of n-elements. Given array is a permutation of some Arithmetic Progression. Find the minimum number of De-arrangements present in that array so as to make that array an Arithmetic progression.
Examples:
Input : arr[] = [8, 6, 10 ,4, 2]
Output : Minimum De-arrangement = 3
Explanation : arr[] = [10, 8, 6, 4, 2] is permutation
which forms an AP and has minimum de-arrangements.
Input : arr[] = [5, 10, 15, 25, 20]
Output : Minimum De-arrangement = 2
Explanation : arr[] = [5, 10, 15, 20, 25] is permutation
which forms an AP and has minimum de-arrangements.
As per property of Arithmetic Progression our sequence will be either in increasing or decreasing manner. Also, we know that reverse of any Arithmetic Progression also form another Arithmetic Progression. So, we create a copy of original array and then once sort our given array in increase order and find total count of mismatch again after that we will reverse our sorted array and found new count of mismatch. Comparing both the counts of mismatch we can find the minimum number of de-arrangements.
C++
// CPP for counting minimum de-arrangements present
// in an array.
#include<bits/stdc++.h>
using namespace std;
// function to count Dearrangement
int countDe (int arr[], int n)
{
// create a copy of original array
vector <int> v (arr, arr+n);
// sort the array
sort(arr, arr+n);
// traverse sorted array for counting mismatches
int count1 = 0;
for (int i=0; i<n; i++)
if (arr[i] != v[i])
count1++;
// reverse the sorted array
reverse(arr,arr+n);
// traverse reverse sorted array for counting
// mismatches
int count2 = 0;
for (int i=0; i<n; i++)
if (arr[i] != v[i])
count2++;
// return minimum mismatch count
return (min (count1, count2));
}
// driver program
int main()
{
int arr[] = {5, 9, 21, 17, 13};
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Minimum Dearrangement = " << countDe(arr, n);
return 0;
}
Java
// Java code for counting minimum
// de-arrangements present in an array.
import java.util.*;
import java.lang.*;
import java.util.Arrays;
public class GeeksforGeeks{
// function to count Dearrangement
public static int countDe(int arr[], int n){
int v[] = new int[n];
// create a copy of original array
for(int i = 0; i < n; i++)
v[i] = arr[i];
// sort the array
Arrays.sort(arr);
// traverse sorted array for
// counting mismatches
int count1 = 0;
for (int i = 0; i < n; i++)
if (arr[i] != v[i])
count1++;
// reverse the sorted array
Collections.reverse(Arrays.asList(arr));
// traverse reverse sorted array
// for counting mismatches
int count2 = 0;
for (int i = 0; i < n; i++)
if (arr[i] != v[i])
count2++;
// return minimum mismatch count
return (Math.min (count1, count2));
}
// driver code
public static void main(String argc[]){
int arr[] = {5, 9, 21, 17, 13};
int n = 5;
System.out.println("Minimum Dearrangement = "+
countDe(arr, n));
}
}
/*This code is contributed by Sagar Shukla.*/
Python3
# Python3 code for counting minimum
# de-arrangements present in an array.
# function to count Dearrangement
def countDe(arr, n):
i = 0
# create a copy of
# original array
v = arr.copy()
# sort the array
arr.sort()
# traverse sorted array for
# counting mismatches
count1 = 0
i = 0
while( i < n ):
if (arr[i] != v[i]):
count1 = count1 + 1
i = i + 1
# reverse the sorted array
arr.sort(reverse=True)
# traverse reverse sorted array
# for counting mismatches
count2 = 0
i = 0
while( i < n ):
if (arr[i] != v[i]):
count2 = count2 + 1
i = i + 1
# return minimum mismatch count
return (min (count1, count2))
# Driven code
arr = [5, 9, 21, 17, 13]
n = 5
print ("Minimum Dearrangement =",countDe(arr, n))
# This code is contributed by "rishabh_jain".
C#
// C# code for counting
// minimum de-arrangements
// present in an array.
using System;
class GFG
{
// function to count
// Dearrangement
public static int countDe(int[] arr,
int n)
{
int[] v = new int[n];
// create a copy
// of original array
for(int i = 0; i < n; i++)
v[i] = arr[i];
// sort the array
Array.Sort(arr);
// traverse sorted array for
// counting mismatches
int count1 = 0;
for (int i = 0; i < n; i++)
if (arr[i] != v[i])
count1++;
// reverse the sorted array
Array.Reverse(arr);
// traverse reverse sorted array
// for counting mismatches
int count2 = 0;
for (int i = 0; i < n; i++)
if (arr[i] != v[i])
count2++;
// return minimum
// mismatch count
return (Math.Min (count1, count2));
}
// Driver code
public static void Main()
{
int[] arr = new int[]{5, 9, 21, 17, 13};
int n = 5;
Console.WriteLine("Minimum Dearrangement = " +
countDe(arr, n));
}
}
// This code is contributed by mits
PHP
<?php
// PHP for counting minimum de-arrangements
// present in an array.
// function to count Dearrangement
function countDe ($arr, $n)
{
// create a copy of original array
$v = $arr;
// sort the array
sort($arr);
// traverse sorted array for
// counting mismatches
$count1 = 0;
for ($i = 0; $i < $n; $i++)
if ($arr[$i] != $v[$i])
$count1++;
// reverse the sorted array
rsort($arr);
// traverse reverse sorted array
// for counting mismatches
$count2 = 0;
for ($i = 0; $i < $n; $i++)
if ($arr[$i] != $v[$i])
$count2++;
// return minimum mismatch count
return (min($count1, $count2));
}
// Driver Code
$arr = array(5, 9, 21, 17, 13);
$n = count($arr);
echo "Minimum Dearrangement = " .
countDe($arr, $n);
// This code is contributed by mits
?>
JavaScript
<script>
// JavaScript code for counting minimum
// de-arrangements present in an array
// Function to count Dearrangement
function countDe(arr, n)
{
let v = [];
// Create a copy of original array
for(let i = 0; i < n; i++)
v[i] = arr[i];
// Sort the array
arr.sort();
// Traverse sorted array for
// counting mismatches
let count1 = 0;
for(let i = 0; i < n; i++)
if (arr[i] != v[i])
count1++;
// Reverse the sorted array
arr.reverse();
// Traverse reverse sorted array
// for counting mismatches
let count2 = 0;
for(let i = 0; i < n; i++)
if (arr[i] != v[i])
count2++;
// Return minimum mismatch count
return (Math.min (count1, count2));
}
// Driver Code
let arr = [ 5, 9, 21, 17, 13 ];
let n = 5;
document.write("Minimum Dearrangement = " +
countDe(arr, n));
// This code is contributed by sanjoy_62
</script>
Output:
Minimum Dearrangement = 2
Time Complexity: O(nlogn).
Auxiliary Space: O(n)
Similar Reads
Change one element in the given array to make it an Arithmetic Progression Given an array which is an original arithmetic progression with one element changed. The task is to make it an arithmetic progression again. If there are many such possible sequences, return any one of them. The length of the array will always be greater than 2. Examples: Input : arr = [1, 3, 4, 7]
7 min read
De-arrangements for minimum product sum of two arrays Given two arrays A[] and B[] of same size n. We need to first permute any of arrays such that the sum of product of pairs( 1 element from each) is minimum. That is SUM ( Ai*Bi) for all i is minimum. We also need to count number of de-arrangements present in original array as compared to permuted arr
5 min read
Find elements in Array whose positions forms Arithmetic Progression Given an array A[] of N integers. Consider an integer num such that num occurs in the array A[] and all the positions of num, sorted in increasing order forms an arithmetic progression. The task is to print all such pairs of num along with the common difference of the arithmetic progressions they fo
11 min read
Count of AP (Arithmetic Progression) Subsequences in an array Given an array arr[] of n positive integers. The task is to count the number of Arithmetic Progression subsequences in the array. Note: Empty sequence or single element sequence is Arithmetic Progression.Examples: Input: arr[] = [1, 2, 3]Output: 8Explanation: Arithmetic Progression subsequence from
15+ min read
Minimize Nth term of an Arithmetic progression (AP) Given two integers A, B which are any two terms of an Arithmetic Progression series, and an integer N, the task is to minimize the Nth term of that arithmetic progression. Note: All the elements of an AP series must be positive. Examples: Input: A = 1, B = 6, N = 3Output: 11Explanations: First three
7 min read