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.
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. Time Complexity = O(nlogn).
Minimum Dearrangement = 2
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Minimum elements inserted in a sorted array to form an Arithmetic progression
- Check whether Arithmetic Progression can be formed from the given array
- Count of AP (Arithmetic Progression) Subsequences in an array
- Convert given array to Arithmetic Progression by adding an element
- Change one element in the given array to make it an Arithmetic Progression
- Arithmetic Progression
- Longest Arithmetic Progression | DP-35
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Find the missing number in Arithmetic Progression
- Program for N-th term of Arithmetic Progression series
- Program to print Arithmetic Progression series
- Longest arithmetic progression with the given common difference
- Count common elements in two arrays which are in Arithmetic Progression
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression