📌  相关文章
📜  具有最大相邻元素之间差异总和的字典序最小排列

📅  最后修改于: 2021-09-07 02:26:22             🧑  作者: Mango

给定一个大小为N的数组arr[] ,任务是找到给定数组的字典序最小排列,使得相邻元素之间的差异之和最大。

例子:

朴素的方法:最简单的方法是生成给定数组的所有排列并找到每个排列的总和,同时跟踪获得的最大总和。最后,用最大和打印字典序最小的排列。

时间复杂度: O(N * N!)
辅助空间: O(1)

高效方法:上述方法可以基于以下观察进行优化:

为了最大化Sp 1应该是最大的元素, p N 应该是给定数组arr[] 中的最小元素。要构建按字典顺序排列的最小排列,请按递增顺序排列其余元素。请按照以下步骤解决问题:

  • 按升序对数组arr[]进行排序。
  • 交换第一个数组元素,即a[0]和最后一个数组元素,即arr[N – 1]
  • 完成上述步骤后,打印修改后的数组arr[]

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the lexicographically
// smallest permutation of an array such
// that the sum of the difference between
// adjacent elements is maximum
void maximumSumPermutation(vector& arr)
{
 
    // Stores the size of the array
    int N = arr.size();
 
    // Sort the given array in
    // incresing order
    sort(arr.begin(), arr.end());
 
    // Swap the first and last array elements
    swap(arr[0], arr[N - 1]);
 
    // Print the required permutation
    for (int i : arr) {
 
        cout << i << " ";
    }
}
 
// Driver Code
int main()
{
    vector arr = { 1, 2, 3, 4, 5 };
    maximumSumPermutation(arr);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
 
    // Function to find the lexicographically
    // smallest permutation of an array such
    // that the sum of the difference between
    // adjacent elements is maximum
    static void maximumSumPermutation(int[] arr)
    {
 
        // Stores the size of the array
        int N = arr.length;
 
        // Sort the given array in
        // incresing order
        Arrays.sort(arr);
 
        // Swap the first and last array elements
          int temp = arr[0];
          arr[0] = arr[N - 1];
        arr[N - 1] = temp;
 
        // Print the required permutation
        for (int i : arr) {
 
            System.out.print(i + " ");
        }
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 1, 2, 3, 4, 5 };
        maximumSumPermutation(arr);
    }
}
 
// This code is contributed by Dharanendra L V.


Python3
# Python program for the above approach
 
# Function to find the lexicographically
# smallest permutation of an array such
# that the sum of the difference between
# adjacent elements is maximum
def maximumSumPermutation(arr):
   
    # Stores the size of the array
    N = len(arr);
 
    # Sort the given array in
    # incresing order
    arr.sort();
 
    # Swap the first and last array elements
    temp = arr[0];
    arr[0] = arr[N - 1];
    arr[N - 1] = temp;
 
    # Print the required permutation
    for i in arr:
        print(i, end = " ");
 
# Driver Code
if __name__ == '__main__':
    arr = [1, 2, 3, 4, 5];
    maximumSumPermutation(arr);
 
# This code is contributed by 29AjayKumar


C#
// C# program to implement
// the above approach
using System;
class GFG{
 
  // Function to find the lexicographically
  // smallest permutation of an array such
  // that the sum of the difference between
  // adjacent elements is maximum
  static void maximumSumPermutation(int[] arr)
  {
 
    // Stores the size of the array
    int N = arr.Length;
 
    // Sort the given array in
    // incresing order
    Array.Sort(arr);
 
    // Swap the first and last array elements
    int temp = arr[0];
    arr[0] = arr[N - 1];
    arr[N - 1] = temp;
 
    // Print the required permutation
    foreach (int i in arr)
    {
      Console.Write(i + " ");
    }
  }
 
 
  // Driver Code
  public static void Main(String[] args)
  {
    int[] arr = { 1, 2, 3, 4, 5 };
    maximumSumPermutation(arr);
  }
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
5 2 3 4 1

时间复杂度: O(N*log N)
辅助空间: O(1)

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