Given a circular array arr of N integers, the task is to minimize the maximum absolute difference of adjacent elements of the array without any removals.
Examples:
Input: arr[] = {1, 3, 10, 2, 0, 9, 6}
Output: {0, 2, 6, 10, 9, 3, 1}
Explanation: In the above example, the maximum difference between adjacent elements is 6, which is between 9 and 3. Other orderings won’t be able to further minimize it.Input: arr[] = {1, 2, 3, 4, 5, 6}
Output: {1, 3, 5, 6, 4, 2}
Example: The maximum difference is 2 between (1, 3) and (3, 5) and (6, 4) and (4, 2).
Approach:
In order to solve the problem, just displaying the sorted array would lead to an incorrect solution as it is treated as a circular array. After sorting, the last and first indexed elements are the highest and lowest elements in the array respectively. Thus, the maximum difference between adjacent elements can be further minimized. So, after sorting, we need to reorder the sorted array such that the even indexed elements precede the odd indexed elements of the array and arrange the odd indexed elements in reverse order.
Illustration: For the given array arr[] = {1, 3, 10, 2, 0, 9, 6}, the sorted array will be {0, 1, 2, 3, 6, 9, 10}. The maximum difference between adjacent elements in the cirular array is |10 – 0| = 10. After reordering the array based on the above approach, we get the array to be {0, 2, 6, 10, 9, 3, 1}. Thus, the maximum difference is now minimized to |9 – 3| = 6.
Below code is the implementation of the above approach:
C++
// C++ Program to minimize the // maximum absolute difference // between adjacent elements // of the circular array #include <bits/stdc++.h> using namespace std; #define ll long long // Function to print the reordered array // which minimizes thee maximum absolute // difference of adjacent elements void solve(vector<int>& arr, int N) { // Sort the given array sort(arr.begin(), arr.end()); // Reorder the array int fl = 1,k=0; for(int i=0;i<=N/2;i++) { if((i%2 && fl) || !fl) { int x = arr[i]; arr.erase(arr.begin() + i); arr.insert(arr.begin() + N - 1 - k, x); k++; fl = 0; } } // Print the new ordering for (int i : arr) cout << i << " "; } // Driver code int main() { int N = 7; vector<int> arr = {1, 3, 10, 2, 0, 9, 6}; solve(arr, N); return 0; } // this code is contributed by divyanshu gupta |
Python 3
# Python 3 Program to minimize the # maximum absolute difference # between adjacent elements # of the circular array # Function to print the reordered array # which minimizes thee maximum absolute # difference of adjacent elements def solve(arr, N): # Sort the given array arr.sort(reverse = False) # Reorder the array fl = 1 k=0 for i in range(N // 2 + 1): if((i % 2 and fl) or fl == 0): x = arr[i] arr.remove(arr[i]) arr.insert(N - 1 - k, x) k += 1 fl = 0 # Print the new ordering for i in arr: print(i, end = " ") # Driver code if __name__ == '__main__': N = 7 arr = [ 1, 3, 10, 2, 0, 9, 6 ] solve(arr, N) # This code is contributed by Samarth |
0 2 6 10 9 3 1
Recommended Posts:
- Minimum absolute difference of adjacent elements in a circular array
- Minimize the maximum difference between adjacent elements in an array
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Minimize the maximum difference of adjacent elements after at most K insertions
- Maximum sum in circular array such that no two elements are adjacent
- Maximum sum in circular array such that no two elements are adjacent | Set 2
- Sort elements of an array in increasing order of absolute difference of adjacent elements
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Sort an Array based on the absolute difference of adjacent elements
- Sort array such that absolute difference of adjacent elements is in increasing order
- Maximum absolute difference between distinct elements in an Array
- Find K elements whose absolute difference with median of array is maximum
- Count maximum elements of an array whose absolute difference does not exceed K
- Arrange first N natural numbers such that absolute difference between all adjacent elements > 1
- Minimize the cost to make all the adjacent elements distinct in an Array
- Minimize sum of adjacent difference with removal of one element from array
- Maximize removal of adjacent array elements based on their absolute value
- Minimize the difference between minimum and maximum elements
- Find maximum number of elements such that their absolute difference is less than or equal to 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.
Improved By : ipg2016107


