给定大小为N的数组arr [] ,任务是找到给定数组在字典上最小的排列,以使相邻元素之间的差之和最大。
例子:
Input: arr[] = {1, 2, 3, 4, 5}
Output: 5 2 3 4 1
Explanation:
Sum of difference between adjacent elements = (5 – 2) + (2 – 3) + (3 – 4) + (4 – 1) = 4.
{5, 2, 3, 4, 1} is lexicographically the smallest array and 4 is the maximum sum possible.
Input: arr[] = {3, 4, 1}
Output: 4 3 1
Explanation:
Sum of the difference between adjacent elements = (4 – 3) + (3 – 1) = 3
{4, 3, 1} is the lexicographically smallest permutation of array elements possible. Maximum sum of adjacent elements possible is 3.
天真的方法:最简单的方法是生成给定数组的所有排列并找到每个排列的和,同时跟踪获得的最大和。最后,以最大的总和打印按字典顺序排列的最小排列。
时间复杂度: O(N * N!)
辅助空间: O(1)
高效的方法:可以基于以下观察来优化上述方法:
Let the required permutation of the array be {p1, p2, p3, …, pN – 2, pN – 1, pN}.
Sum of the difference of the adjacent elements, S = (p1-p2) + (p2-p3) +….+ (pN – 2 – pN – 1) + (pN – 1 – pN)
= p1 – pn
为了使S最大化, p 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;
# Prthe 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.
5 2 3 4 1
时间复杂度: O(N * log N)
辅助空间: O(1)