📌  相关文章
📜  最小化圆形数组中相邻元素的最大绝对差

📅  最后修改于: 2021-09-06 06:10:05             🧑  作者: Mango

给定一个由N 个整数组成的圆形数组arr ,任务是在不进行任何删除的情况下最小化数组相邻元素的最大绝对差。

例子:

方法
为了解决这个问题,只显示已排序的数组会导致错误的解决方案,因为它被视为圆形数组。排序后,最后和第一个索引元素分别是数组中最高和最低的元素。因此,可以进一步最小化相邻元素之间的最大差异。因此,在排序之后,我们需要对排序后的数组重新排序,使得偶数索引元素在数组的奇数索引元素之前,并以相反的顺序排列奇数索引元素。

下面的代码是上述方法的实现:

C++
// C++ Program to minimize the
// maximum absolute difference
// between adjacent elements
// of the circular array
 
#include 
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& 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 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 thee maximum absolute
// difference of adjacent elements
static void solve(Vector 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 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 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


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 thee maximum
// absolute difference of
// adjacent elements
static void solve(List 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 arr = new List();
 
  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


输出:
0 2 6 10 9 3 1






如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live