📌  相关文章
📜  给定数组的可能旋转计数以从前半部分删除最大元素

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

给定数组的可能旋转计数以从前半部分删除最大元素

给定一个偶数长度N的数组arr[ ] ,任务是找到这个数组可能的循环移位(旋转)的数量,使得数组的前半部分 不包含最大元素。

例子:

朴素的方法:解决这个问题的最基本的方法是基于以下思想:

时间复杂度: O(N 3 ),其中 N^2 用于旋转,N 用于在每次旋转中找到最大值。
辅助空间: O(1)

有效的方法:解决问题的想法是通过遍历数组和一些基本的数学概念。

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

  • 首先,找到最大元素
  • 然后找到最大元素对之间的最大范围。
  • 长度为m的范围将max(m-frac(N/2)+1, 0)添加到答案中。

下面是上述方法的实现:

C++
// C++ program for the above approach.
 
#include 
using namespace std;
 
// Function to find the number of cyclic shifts
int find(int arr[], int N)
{
    int maxele = *max_element(arr, arr + N);
    int left = -1;
    int right = -1;
 
    // Placing left pointer
    // On its correct position
    for (int i = 0; i < N; i++) {
        if (arr[i] == maxele) {
            left = i;
            break;
        }
    }
 
    // Placing right pointer
    // On its correct position
    for (int i = N - 1; i >= 0; i--) {
        if (arr[i] == maxele) {
            right = i;
            break;
        }
    }
    int ans = (N / 2) - (right - left);
    if (ans <= 0) {
        return 0;
    }
    else {
        return ans;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 3, 5, 3, 3, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function call
    cout << find(arr, N);
    return 0;
}


Java
// Java program for the above approach.
import java.io.*;
class GFG {
 
  // Function to find the number of cyclic shifts
  static int find(int arr[], int N)
  {
    int maxele = Integer.MIN_VALUE;
    for(int i  = 0; i < N; i++){
      maxele = Math.max(arr[i], maxele);
    }
    int left = -1;
    int right = -1;
 
    // Placing left pointer
    // On its correct position
    for (int i = 0; i < N; i++) {
      if (arr[i] == maxele) {
        left = i;
        break;
      }
    }
 
    // Placing right pointer
    // On its correct position
    for (int i = N - 1; i >= 0; i--) {
      if (arr[i] == maxele) {
        right = i;
        break;
      }
    }
    int ans = (N / 2) - (right - left);
    if (ans <= 0) {
      return 0;
    }
    else {
      return ans;
    }
  }
 
  // Driver Code
  public static void main (String[] args) {
    int arr[] = { 3, 3, 5, 3, 3, 3 };
    int N = arr.length;
 
    // Function call
    System.out.print(find(arr, N));
  }
}
 
// This code is contributed by hrithikgarg03188.


Python3
# Python code for the above approach
 
# Function to find the number of cyclic shifts
def find(arr, N):
    maxele = max(arr)
    left = -1
    right = -1
 
    # Placing left pointer
    # On its correct position
    for i in range(N):
        if (arr[i] == maxele):
            left = i
            break
 
    # Placing right pointer
    # On its correct position
    for i in range(N - 1, -1, -1):
        if (arr[i] == maxele):
            right = i
            break
    ans = (N // 2) - (right - left)
    if (ans <= 0):
        return 0
    else:
        return ans
 
# Driver Code
arr = [3, 3, 5, 3, 3, 3]
N = len(arr)
 
# Function call
print(find(arr, N))
 
# This code is contributed by shinjanpatra


C#
// C# program for the above approach.
using System;
class GFG {
 
    // Function to find the number of cyclic shifts
    static int find(int[] arr, int N)
    {
        int maxele = Int32.MinValue;
        for (int i = 0; i < N; i++) {
            maxele = Math.Max(arr[i], maxele);
        }
        int left = -1;
        int right = -1;
 
        // Placing left pointer
        // On its correct position
        for (int i = 0; i < N; i++) {
            if (arr[i] == maxele) {
                left = i;
                break;
            }
        }
 
        // Placing right pointer
        // On its correct position
        for (int i = N - 1; i >= 0; i--) {
            if (arr[i] == maxele) {
                right = i;
                break;
            }
        }
        int ans = (N / 2) - (right - left);
        if (ans <= 0) {
            return 0;
        }
        else {
            return ans;
        }
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 3, 3, 5, 3, 3, 3 };
        int N = arr.Length;
 
        // Function call
        Console.Write(find(arr, N));
    }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript



输出
3

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