重新排列数组以最大化小于其相邻元素的元素
给定一个由N个不同整数组成的数组arr[] ,任务是重新排列数组元素,使得小于其相邻元素的元素的数量最大。
注意:索引0左侧和索引N-1右侧的元素被视为-INF。
例子:
Input: arr[] = {1, 2, 3, 4}
Output: 2 1 3 4
Explanation:
One possible way to rearrange is as {2, 1, 3, 4}.
- For arr[0](= 2), is greater than the elements on both sides of it.
- For arr[1](= 1), is less than the elements on both sides of it.
- For arr[2](= 3), is less than the elements on its right and greater than the elements on its left.
- For arr[4](= 1), is greater than the elements on both sides of it.
Therefore, there is a total of 1 array element satisfying the condition in the above arrangement. And it is the maximum possible.
Input: arr[] = {2, 7}
Output: 2 7
方法:可以根据以下观察解决给定的问题:
- Consider the maximum number of indices that can satisfy the conditions being X.
- Then, at least (X + 1) larger elements are required to make it possible as each element requires 2 greater elements.
- Therefore, the maximum number of elements that is smaller than their adjacent is given by (N – 1)/2.
请按照以下步骤解决问题:
- 初始化一个数组,比如temp[]的大小N ,它存储重新排列的数组。
- 按升序对给定数组arr[]进行排序。
- 从数组arr[]中选择前(N – 1)/2 个元素,并将它们放在数组temp[]中的奇数索引处。
- 从数组arr[]中选择剩余的(N + 1)/2 个元素,并将它们放在数组temp[]的剩余索引中。
- 最后,完成上述步骤后,将数组temp[]打印为结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to rearrange array such that
// count of element that are smaller than
// their adjacent elements is maximum
void maximumIndices(int arr[], int N)
{
// Stores the rearranged array
int temp[N] = { 0 };
// Stores the maximum count of
// elements
int maxIndices = (N - 1) / 2;
// Sort the given array
sort(arr, arr + N);
// Place the smallest (N - 1)/2
// elements at odd indices
for (int i = 0; i < maxIndices; i++) {
temp[2 * i + 1] = arr[i];
}
// Placing the rest of the elements
// at remaining indices
int j = 0;
for (int i = maxIndices; i < N;) {
// If no element of the array
// has been placed here
if (temp[j] == 0) {
temp[j] = arr[i];
i++;
}
j++;
}
// Print the resultant array
for (int i = 0; i < N; i++) {
cout << temp[i] << " ";
}
}
// Driver Code
int main()
{
// Input
int arr[] = { 1, 2, 3, 4 };
int N = sizeof(arr) / sizeof(arr[0]);
// Function call
maximumIndices(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to rearrange array such that
// count of element that are smaller than
// their adjacent elements is maximum
public static void maximumIndices(int arr[], int N)
{
// Stores the rearranged array
int[] temp = new int[N];
// Stores the maximum count of
// elements
int maxIndices = (N - 1) / 2;
// Sort the given array
Arrays.sort(arr);
// Place the smallest (N - 1)/2
// elements at odd indices
for (int i = 0; i < maxIndices; i++) {
temp[2 * i + 1] = arr[i];
}
// Placing the rest of the elements
// at remaining indices
int j = 0;
for (int i = maxIndices; i < N;) {
// If no element of the array
// has been placed here
if (temp[j] == 0) {
temp[j] = arr[i];
i++;
}
j++;
}
// Print the resultant array
for (int i = 0; i < N; i++) {
System.out.print(temp[i] + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Input
int arr[] = { 1, 2, 3, 4 };
int N = 4;
// Function call
maximumIndices(arr, N);
}
}
// This code is contributed by RohitOberoi.
Python3
# Python program for the above approach
# Function to rearrange array such that
# count of element that are smaller than
# their adjacent elements is maximum
def maximumIndices(arr, N):
# Stores the rearranged array
temp = [0] * N
# Stores the maximum count of
# elements
maxIndices = (N - 1) // 2
# Sort the given array
arr.sort()
# Place the smallest (N - 1)/2
# elements at odd indices
for i in range(maxIndices):
temp[2 * i + 1] = arr[i]
# Placing the rest of the elements
# at remaining indices
j = 0
i = maxIndices
while(i < N):
# If no element of the array
# has been placed here
if (temp[j] == 0):
temp[j] = arr[i]
i += 1
j += 1
# Print the resultant array
for i in range(N):
print(temp[i], end=" ")
# Driver Code
# Input
arr = [1, 2, 3, 4]
N = len(arr)
# Function call
maximumIndices(arr, N)
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
class GFG{
// Function to rearrange array such that
// count of element that are smaller than
// their adjacent elements is maximum
public static void maximumIndices(int []arr, int N)
{
// Stores the rearranged array
int[] temp = new int[N];
// Stores the maximum count of
// elements
int maxIndices = (N - 1) / 2;
// Sort the given array
Array.Sort(arr);
// Place the smallest (N - 1)/2
// elements at odd indices
for(int i = 0; i < maxIndices; i++)
{
temp[2 * i + 1] = arr[i];
}
// Placing the rest of the elements
// at remaining indices
int j = 0;
for(int i = maxIndices; i < N;)
{
// If no element of the array
// has been placed here
if (temp[j] == 0)
{
temp[j] = arr[i];
i++;
}
j++;
}
// Print the resultant array
for(int i = 0; i < N; i++)
{
Console.Write(temp[i] + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Input
int []arr = { 1, 2, 3, 4 };
int N = 4;
// Function call
maximumIndices(arr, N);
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出
2 1 3 4
时间复杂度: O(N*log(N))
辅助空间: O(N)