📌  相关文章
📜  重新排列数组以最大化局部最小值的数量

📅  最后修改于: 2021-05-17 22:38:17             🧑  作者: Mango

给定大小为N的数组arr [] ,任务是重新排列数组元素,以使数组中局部最小值的数量最大。

注意:如果一个元素arr [x]小于或等于它的两个相邻元素,则称其为局部最小值。第一个和最后一个数组元素不能视为局部最小值。

例子:

方法:想法是使用排序算法和两个指针技术来解决此问题。请按照以下步骤解决此问题:

  • 以升序对数组进行排序。
  • 初始化两个变量,例如left = 0right = N / 2 ,分别存储左指针和右指针的索引。
  • 遍历数组,并在每次遍历时,首先打印arr [right]的值,然后打印arr [left]的值,并将leftright的值增加1

下面是上述方法的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
 
// Function to rearrange array elements to
// maximize count of local minima in the array
void rearrangeArrMaxcntMinima(int arr[], int N)
{
    // Sort the array in
    // ascending order
    sort(arr, arr + N);
 
    // Stores index of
    // left pointer
    int left = 0;
 
    // Stores index of
    // right pointer
    int right = N / 2;
 
    // Traverse the array elements
    while (left < N / 2 || right < N) {
 
        // if right is less than N
        if (right < N) {
 
            // Print array element
            cout << arr[right] << " ";
 
            // Update right
            right++;
        }
 
        if (left < N / 2) {
 
            // Print array element
            cout << arr[left] << " ";
 
            // Update left
            left++;
        }
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { 1, 2, 3, 4, 5 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    rearrangeArrMaxcntMinima(arr, N);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
  
// Function to rearrange array elements to
// maximize count of local minima in the array
static void rearrangeArrMaxcntMinima(int arr[],
                                     int N)
{
     
    // Sort the array in
    // ascending order
    Arrays.sort(arr);
  
    // Stores index of
    // left pointer
    int left = 0;
  
    // Stores index of
    // right pointer
    int right = N / 2;
  
    // Traverse the array elements
    while (left < N / 2 || right < N)
    {
         
        // If right is less than N
        if (right < N)
        {
             
            // Print array element
            System.out.print(arr[right] + " ");
  
            // Update right
            right++;
        }
  
        if (left < N / 2)
        {
             
            // Print array element
            System.out.print(arr[left] + " ");
  
            // Update left
            left++;
        }
    }
}
  
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
  
    int N = arr.length;
  
    rearrangeArrMaxcntMinima(arr, N);
}
}
 
// This code is contributed by code_hunt


Python3
# Python3 program to implement
# the above approach
 
# Function to rearrange array
# elements to maximize count of
# local minima in the array
def rearrangeArrMaxcntMinima(arr, N):
 
    # Sort the array in
    # ascending order
    arr.sort()
 
    # Stores index of
    # left pointer
    left = 0
 
    # Stores index of
    # right pointer
    right = N // 2
 
    # Traverse the array elements
    while (left < N // 2 or
           right < N):
 
        # if right is less
        # than N
        if (right < N):
 
            # Print array element
            print(arr[right],
                  end = " ")
 
            # Update right
            right += 1
 
        if (left < N // 2):
 
            # Print array element
            print(arr[left],
                  end = " ")
 
            # Update left
            left += 1
 
# Driver Code
if __name__ == "__main__":
   
    arr = [1, 2, 3, 4, 5]
    N = len(arr)
    rearrangeArrMaxcntMinima(arr, N)
 
# This code is contributed by Chitranayal


C#
// C# program to implement
// the above approach
using System;
 
class GFG{
  
// Function to rearrange array elements to
// maximize count of local minima in the array
static void rearrangeArrMaxcntMinima(int []arr,
                                     int N)
{
   
    // Sort the array in
    // ascending order
    Array.Sort(arr);
  
    // Stores index of
    // left pointer
    int left = 0;
  
    // Stores index of
    // right pointer
    int right = N / 2;
  
    // Traverse the array elements
    while (left < N / 2 || right < N)
    {
       
        // If right is less than N
        if (right < N)
        {
             
            // Print array element
            Console.Write(arr[right] + " ");
  
            // Update right
            right++;
        }
        if (left < N / 2)
        {
             
            // Print array element
            Console.Write(arr[left] + " ");
  
            // Update left
            left++;
        }
    }
}
  
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
  
    rearrangeArrMaxcntMinima(arr, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
3 1 4 2 5









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