![]() |
VOOZH | about |
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.
Output:
Minimum Dearrangement = 2
Time Complexity: O(nlogn).
Auxiliary Space: O(n)