给定一个包含N 个整数的数组arr[] ,任务是重新排列数组的所有元素,使得数组中连续元素之间的绝对差按升序排序。
例子
Input: arr[] = { 5, -2, 4, 8, 6, 5 }
Output: 5 5 6 4 8 -2
Explanation:
|5 – 5| = 0
|5 – 6| = 1
|6 – 4| = 2
|4 – 8| = 4
|8 – (-2)| = 10
Hence, the differences between adjacent elements are sorted.
Input: arr[] = { 8, 1, 4, 2 }
Output: 4 2 8 1
Explanation:
|2 – 4| = 2
|8 – 2| = 6
|1 – 8| = 7
Hence, the differences between adjacent elements are sorted.
方法:该问题可以使用贪心方法解决。我们知道最大差值在数组的最小元素和最大元素之间。使用这个事实,如果我们在答案中包含最小元素之一,那么包含在答案数组中的下一个元素将是最大元素,然后包含的第三个元素将是第二个最小元素,然后包含的第四个元素将是第二个最大值等将给出所需的数组。
以下是步骤:
- 按升序对给定数组arr[]进行排序。
- 从排序数组中选择第一个最大值(比如a )和最小元素(比如b )并插入答案数组(比如ans[] )作为 {a, b}。
- 通过从排序数组中选择第二个、第三个、第四个……最大和最小元素来重复上述步骤,并将其插入到答案数组的前面。
- 完成上述所有操作后,答案数组就有了想要的结果。
下面是上述方法的实现:
CPP
// C++ implementation of the above approach
#include
using namespace std;
// Function that arrange the array such that
// all absolute difference between adjacent
// element are sorted
void sortedAdjacentDifferences(int arr[], int n)
{
// To store the resultant array
int ans[n];
// Sorting the given array
// in ascending order
sort(arr + 0, arr + n);
// Variable to represent left and right
// ends of the given array
int l = 0, r = n - 1;
// Traversing the answer array in reverse
// order and arrange the array elements from
// arr[] in reverse order
for (int i = n - 1; i >= 0; i--) {
// Inserting elements in zig-zag manner
if (i % 2) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
// Displaying the resultant array
for (int i = 0; i < n; i++) {
cout << ans[i] << " ";
}
}
// Driver Code
int main()
{
int arr[] = { 5, -2, 4, 8, 6, 4, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function Call
sortedAdjacentDifferences(arr, n);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG{
// Function that arrange the array such that
// all absolute difference between adjacent
// element are sorted
static void sortedAdjacentDifferences(int arr[], int n)
{
// To store the resultant array
int []ans = new int[n];
// Sorting the given array
// in ascending order
Arrays.sort(arr);
// Variable to represent left and right
// ends of the given array
int l = 0, r = n - 1;
// Traversing the answer array in reverse
// order and arrange the array elements from
// arr[] in reverse order
for (int i = n - 1; i >= 0; i--) {
// Inserting elements in zig-zag manner
if (i % 2 == 1) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
// Displaying the resultant array
for (int i = 0; i < n; i++) {
System.out.print(ans[i]+ " ");
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, -2, 4, 8, 6, 4, 5 };
int n = arr.length;
// Function Call
sortedAdjacentDifferences(arr, n);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 implementation of the above approach
# Function that arrange the array such that
# all absolute difference between adjacent
# element are sorted
def sortedAdjacentDifferences(arr, n):
# To store the resultant array
ans = [0]*n
# Sorting the given array
# in ascending order
arr = sorted(arr)
# Variable to represent left and right
# ends of the given array
l = 0
r = n - 1
# Traversing the answer array in reverse
# order and arrange the array elements from
# arr[] in reverse order
for i in range(n - 1, -1, -1):
# Inserting elements in zig-zag manner
if (i % 2):
ans[i] = arr[l]
l += 1
else:
ans[i] = arr[r]
r -= 1
# Displaying the resultant array
for i in range(n):
print(ans[i], end=" ")
# Driver Code
if __name__ == '__main__':
arr=[5, -2, 4, 8, 6, 4, 5]
n = len(arr)
# Function Call
sortedAdjacentDifferences(arr, n)
# This code is contributed by mohit kumar 29
C#
// C# implementation of the above approach
using System;
class GFG
{
// Function that arrange the array such that
// all absolute difference between adjacent
// element are sorted
static void sortedAdjacentDifferences(int[] arr, int n)
{
// To store the resultant array
int[] ans = new int[n];
// Sorting the given array
// in ascending order
Array.Sort(arr);
// Variable to represent left and right
// ends of the given array
int l = 0, r = n - 1;
// Traversing the answer array in reverse
// order and arrange the array elements from
// arr[] in reverse order
for (int i = n - 1; i >= 0; i--) {
// Inserting elements in zig-zag manner
if (i % 2 != 0) {
ans[i] = arr[l];
l++;
}
else {
ans[i] = arr[r];
r--;
}
}
// Displaying the resultant array
for (int i = 0; i < n; i++) {
Console.Write(ans[i] + " ");
}
}
// Driver Code
public static void Main()
{
int[] arr = { 5, -2, 4, 8, 6, 4, 5 };
int n = arr.Length;
// Function Call
sortedAdjacentDifferences(arr, n);
}
}
// This code is contributed by chitranayal
Javascript
5 4 5 4 6 -2 8
时间复杂度: O(N*log N),其中 N 是给定数组中的元素数。
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live