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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- 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
- Longest Arithmetic Progression | DP-35
- Program for N-th term of Arithmetic Progression series
- Program to print Arithmetic Progression series
- PHP program to print an arithmetic progression series using inbuilt functions
- Longest string in non-decreasing order of ASCII code and in arithmetic progression
- Longest arithmetic progression with the given common difference
- Ratio of mth and nth term in an Arithmetic Progression (AP)
- Arithmetic Progression
- Check whether nodes of Binary Tree form Arithmetic, Geometric or Harmonic Progression
- Count common elements in two arrays which are in Arithmetic Progression
- Find the missing number in unordered Arithmetic Progression
- Count of subarrays forming an Arithmetic Progression (AP)
- Longest subarray forming an Arithmetic Progression (AP)
- Arithmetic Progression containing X and Y with least possible first term
- Minimize Nth term of an Arithmetic progression (AP)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.