Minimize the maximum absolute difference of adjacent elements in a circular array
Last Updated :
12 Oct, 2022
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 circular 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 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 the 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
Java
// Java program to minimize the
// maximum absolute difference
// between adjacent elements
// of the circular array
import java.util.*;
class GFG{
// Function to print the reordered array
// which minimizes the maximum absolute
// difference of adjacent elements
static void solve(Vector<Integer> arr, int N)
{
// Sort the given array
Collections.sort(arr);
// Reorder the array
int fl = 1, k = 0;
for(int i = 0; i <= N / 2; i++)
{
if ((i % 2 != 0 && fl != 0) || fl == 0)
{
int x = arr.get(i);
arr.remove(i);
arr.add( N - 1 - k, x);
k++;
fl = 0;
}
}
// Print the new ordering
for(int i : arr)
System.out.print(i + " ");
}
// Driver code
public static void main(String[] args)
{
int N = 7;
Vector<Integer> arr = new Vector<>();
arr.add(1);
arr.add(3);
arr.add(10);
arr.add(2);
arr.add(0);
arr.add(9);
arr.add(6);
solve(arr, N);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 Program to minimize the
# maximum absolute difference
# between adjacent elements
# of the circular array
# Function to print the reordered array
# which minimizes the 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
C#
// C# program to minimize the
// maximum absolute difference
// between adjacent elements
// of the circular array
using System;
using System.Collections.Generic;
class GFG{
// Function to print the
// reordered array which
// minimizes the maximum
// absolute difference of
// adjacent elements
static void solve(List<int> arr,
int N)
{
// Sort the given array
arr.Sort();
// Reorder the array
int fl = 1, k = 0;
for(int i = 0; i <= N / 2; i++)
{
if ((i % 2 != 0 &&
fl != 0) || fl == 0)
{
int x = arr[i];
arr.RemoveAt(i);
arr.Insert(N - 1 - k, x);
k++;
fl = 0;
}
}
// Print the new ordering
foreach(int i in arr)
Console.Write(i + " ");
}
// Driver code
public static void Main(String[] args)
{
int N = 7;
List<int> arr = new List<int>();
arr.Add(1);
arr.Add(3);
arr.Add(10);
arr.Add(2);
arr.Add(0);
arr.Add(9);
arr.Add(6);
solve(arr, N);
}
}
// This code is contributed by Rajput-Ji
JavaScript
<script>
// JavaScript Program to minimize the
// maximum absolute difference
// between adjacent elements
// of the circular array
// Function to print the reordered array
// which minimizes the maximum absolute
// difference of adjacent elements
function solve(arr, N){
// Sort the given array
arr.sort((a,b)=>a-b)
// Reorder the array
let fl = 1
let k=0
for(let i=0;i<(Math.log(N / 2) + 1);i++){
if((i % 2 && fl) || fl == 0){
let x = arr[i]
arr = arr.filter((y)=>y != x)
arr.splice(N - 1 - k,0,x)
k += 1
fl = 0
}
}
// Print the new ordering
for(let i of arr)
document.write(i," ")
}
// Driver code
let N = 7
let arr = [ 1, 3, 10, 2, 0, 9, 6 ]
solve(arr, N)
// This code is contributed by shinjanpatra
</script>
Time complexity: O(N2) where N is the size of the given array
Auxiliary space: O(1)
Similar Reads
Minimum absolute difference of adjacent elements in a Circular Array Given a circular array arr[] of length N, the task is to find the minimum absolute difference between any adjacent pair. If there are many optimum solutions, output any of them. Examples: Input: arr[] = {10, 12, 13, 15, 10} Output: 0Explanation: |10 - 10| = 0 is the minimum possible difference. Inpu
5 min read
Maximize sum of absolute difference between adjacent elements in Array with sum K Given two integers N and K, the task is to maximize the sum of absolute differences between adjacent elements of an array of length N and sum K. Examples: Input: N = 5, K = 10 Output: 20 Explanation: The array arr[] with sum 10 can be {0, 5, 0, 5, 0}, maximizing the sum of absolute difference of adj
4 min read
Minimize the maximum difference of adjacent elements after at most K insertions Given an array of N elements, the task is to minimize the maximum difference of adjacent elements by inserting at most K elements in the array.Examples: Input: arr = [2, 6, 8] K = 1 Output: 2 Explanation: After insertion of 4 in between 2 and 6, the array becomes [2, 4, 6, 8]. In this case the maxim
7 min read
Maximize the absolute difference for all elements in the array Given an array A[] of size N and B[] of size M (M >= N), the task is to construct an array C[] by choosing N integers from B[] such that for every index i, sum of absolute difference between A[i] and C[i] is maximum. Examples: Input: N = 4, M = 6, A[] = {6, 1, 2, 4}, B[] = {3, 5, 1, 7, 2, 3}Outpu
10 min read
Sort an Array based on the absolute difference of adjacent elements Given an array arr[] containing N integers, the task is to rearrange all the elements of array such that absolute difference between consecutive elements of the array are sorted in increasing order.Examples Input: arr[] = { 5, -2, 4, 8, 6, 5 } Output: 5 5 6 4 8 -2 Explanation: |5 - 5| = 0 |5 - 6| =
7 min read