转换给定数组的方法计数,使得数组最大值在前半部分不存在
给定一个偶数大小的数组arr[] N 。任务是计算转换arr[]的方式的数量,以使数组的前半部分不包含最大数量。
例子:
Input: arr[] = {2, 2, 5, 2, 2, 2}
Output: 3
Explanation: Following are the ways where the maximum element 5 is not present in the first half of the array.
[2, 2, 2, 5, 2, 2] when x=1 (shifted to the right by 1)
[2, 2, 2, 2, 5, 2] when x=2 (shifted to the right by 2)
[2, 2, 2, 2, 2, 5] when x=3 (shifted to the right by 3)
[5, 2, 2, 2, 2, 2] when x=4 NOT A VALID CASE
Input: arr[] = {3, 3, 6, 3, 3, 6}
Output: 0
Explanation: No matter how many shifts we perform, the maximum number 6 is always present in the first array.
天真的方法:在arr[]中进行右移并根据给定条件检查每种情况。计算所有可能的方式并打印出来。
时间复杂度: O(N * N)
辅助空间: O(1)
有效方法:这个问题是基于实现的。请按照以下步骤解决给定的问题。
- 取数组arr[]的两半。
- 查找并保存向量中的最大值。
- 取一个变量来存储arr[]的最大值。
- 由于最大值可以在数组中多次出现,所以将最大值的位置保存在前面和最后。
- 如果最大值的位置小于数组大小的一半,那么数组的前半部分不可能有这么大的值。
- 如果不是这种情况,那么可能的方式数将是N/2 –(最后一个位置 – 第一个位置)。
下面是上述方法的实现。
C++
// C++ program for above approach
#include
using namespace std;
// Function to find the number of ways to
// achieve the required array
void countWays(vector& arr)
{
int last_pos = -1;
int front_pos = -1;
int N = arr.size();
int maxi = INT_MIN;
for (int i = 0; i < N; i++) {
maxi = max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
cout << (N / 2 - (last_pos - front_pos));
else
cout << "0";
}
// Driver Code
int main()
{
vector arr = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the number of ways to
// achieve the required array
static void countWays(int arr[])
{
int last_pos = -1;
int front_pos = -1;
int N = arr.length;
int maxi = Integer.MIN_VALUE;
for (int i = 0; i < N; i++) {
maxi = Math.max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
System.out.println(N / 2 - (last_pos - front_pos));
else
System.out.println("0");
}
// Driver Code
public static void main (String[] args) {
int arr[] = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
}
}
// This code is contributed by hrithikgarg03188.
Python3
# python3 program for above approach
INT_MIN = -2147483648
# Function to find the number of ways to
# achieve the required array
def countWays(arr):
last_pos = -1
front_pos = -1
N = len(arr)
maxi = INT_MIN
for i in range(0, N):
maxi = max(maxi, arr[i])
for i in range(0, N):
if (arr[i] == maxi):
front_pos = i
break
for i in range(N - 1, -1, -1):
if (arr[i] == maxi):
last_pos = i
break
if (N // 2 >= (last_pos - front_pos)):
print(N // 2 - (last_pos - front_pos))
else:
print("0")
# Driver Code
if __name__ == "__main__":
arr = [2, 2, 5, 2, 2, 2]
# Function Call
countWays(arr)
# This code is contributed by rakeshsahni
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to find the number of ways to
// achieve the required array
static void countWays(int []arr)
{
int last_pos = -1;
int front_pos = -1;
int N = arr.Length;
int maxi = int.MinValue;
for (int i = 0; i < N; i++) {
maxi = Math.Max(maxi, arr[i]);
}
for (int i = 0; i < N; i++) {
if (arr[i] == maxi) {
front_pos = i;
break;
}
}
for (int i = N - 1; i >= 0; i--) {
if (arr[i] == maxi) {
last_pos = i;
break;
}
}
if (N / 2 >= (last_pos - front_pos))
Console.WriteLine(N / 2 - (last_pos - front_pos));
else
Console.WriteLine("0");
}
// Driver Code
public static void Main(String[] args)
{
int []arr = { 2, 2, 5, 2, 2, 2 };
// Function Call
countWays(arr);
}
}
// This code is contributed by shikhasingrajput
Javascript
3
时间复杂度: 在)
辅助空间: O(1)