📌  相关文章
📜  重新排列数组以最大化小于其相邻元素的元素

📅  最后修改于: 2022-05-13 01:56:07.237000             🧑  作者: Mango

重新排列数组以最大化小于其相邻元素的元素

给定一个由N个不同整数组成的数组arr[] ,任务是重新排列数组元素,使得小于其相邻元素的元素的数量最大。

注意:索引0左侧和索引N-1右侧的元素被视为-INF。

例子:

方法:可以根据以下观察解决给定的问题:

请按照以下步骤解决问题:

  • 初始化一个数组,比如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)