给定一个由N 个不同整数组成的数组arr[] ,任务是找到包含给定数组中最大和最小元素的子数组的数量。
例子:
Input: arr[] = {1, 2, 3, 4}
Output: 1
Explanation:
Only a single subarray {1, 2, 3, 4} consists of both the maximum (= 4) and the minimum (= 1) array elements.
Input: arr[] = {4, 1, 2, 3}
Output: 3
Explanation:
Subrrays {4, 1} , {4, 1, 2}, {4, 1, 2, 3} consists of both the maximum(= 4) and the minimum(= 1) array elements .
朴素的方法:最简单的方法是首先遍历数组并找到数组的最大值和最小值,然后生成给定数组的所有可能子数组。对于每个子数组,检查它是否同时包含最大和最小数组元素。对于所有此类子数组,将计数增加 1。最后,打印此类子数组的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)
高效方法:按照以下步骤优化上述方法:
- 找到最大和最小元素的索引。让i和j是各自的索引,使得i < j 。
- 从索引开始到i并在j之后的索引处结束的所有子数组将包含最大和最小数组元素。
- 因此,子数组起始索引的可能索引为[0, i] (total = i + 1 )。
- 因此,子数组结束索引的可能索引为[j, N – 1] (total = N – j )。
- 因此,子数组的数量由(i + 1) * ( N – j) 给出。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to count subarray
// containing both maximum and
// minimum array elements
int countSubArray(int arr[], int n)
{
// If the length of the
// array is less than 2
if (n < 2)
return n;
// Find the index of maximum element
int i
= max_element(arr, arr + n) - arr;
// Find the index of minimum element
int j
= min_element(arr, arr + n) - arr;
// If i > j, then swap
// the value of i and j
if (i > j)
swap(i, j);
// Return the answer
return (i + 1) * (n - j);
}
// Driver Code
int main()
{
int arr[] = { 4, 1, 2, 3 };
int n = sizeof(arr) / sizeof(arr[0]);
// Function call
cout << countSubArray(arr, n);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG{
// Function to count subarray
// containing both maximum and
// minimum array elements
static int countSubArray(int arr[], int n)
{
// If the length of the
// array is less than 2
if (n < 2)
return n;
// Find the index of maximum element
int i = max_element(arr);
// Find the index of minimum element
int j = min_element(arr);
// If i > j, then swap
// the value of i and j
if (i > j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
// Return the answer
return (i + 1) * (n - j);
}
// Function to return max_element index
static int max_element(int[] arr)
{
int idx = 0;
int max = arr[0];
for(int i = 1; i < arr.length; i++)
{
if(max < arr[i])
{
max = arr[i];
idx = i;
}
}
return idx;
}
// Function to return min_element index
static int min_element(int[] arr)
{
int idx = 0;
int min = arr[0];
for(int i = 1; i < arr.length; i++)
{
if (arr[i] < min)
{
min = arr[i];
idx = i;
}
}
return idx;
}
// Driver Code
public static void main (String[] args)
{
int arr[] = { 4, 1, 2, 3 };
int n = arr.length;
// Function call
System.out.println(countSubArray(arr, n));
}
}
// This code is contributed by offbeat
Python3
# Python3 program for
# the above approach
# Function to count subarray
# containing both maximum and
# minimum array elements
def countSubArray(arr, n):
# If the length of the
# array is less than 2
if (n < 2):
return n;
# Find the index of
# maximum element
i = max_element(arr);
# Find the index of
# minimum element
j = min_element(arr);
# If i > j, then swap
# the value of i and j
if (i > j):
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
# Return the answer
return (i + 1) * (n - j);
# Function to return
# max_element index
def max_element(arr):
idx = 0;
max = arr[0];
for i in range(1, len(arr)):
if (max < arr[i]):
max = arr[i];
idx = i;
return idx;
# Function to return
# min_element index
def min_element(arr):
idx = 0;
min = arr[0];
for i in range(1, len(arr)):
if (arr[i] < min):
min = arr[i];
idx = i;
return idx;
# Driver Code
if __name__ == '__main__':
arr = [4, 1, 2, 3];
n = len(arr);
# Function call
print(countSubArray(arr, n));
# This code is contributed by Rajput-Ji
C#
// C# program for
// the above approach
using System;
class GFG{
// Function to count subarray
// containing both maximum and
// minimum array elements
static int countSubArray(int []arr,
int n)
{
// If the length of the
// array is less than 2
if (n < 2)
return n;
// Find the index of maximum element
int i = max_element(arr);
// Find the index of minimum element
int j = min_element(arr);
// If i > j, then swap
// the value of i and j
if (i > j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
// Return the answer
return (i + 1) * (n - j);
}
// Function to return max_element index
static int max_element(int[] arr)
{
int idx = 0;
int max = arr[0];
for(int i = 1; i < arr.Length; i++)
{
if(max < arr[i])
{
max = arr[i];
idx = i;
}
}
return idx;
}
// Function to return min_element index
static int min_element(int[] arr)
{
int idx = 0;
int min = arr[0];
for(int i = 1; i < arr.Length; i++)
{
if (arr[i] < min)
{
min = arr[i];
idx = i;
}
}
return idx;
}
// Driver Code
public static void Main(String[] args)
{
int []arr = {4, 1, 2, 3};
int n = arr.Length;
// Function call
Console.WriteLine(countSubArray(arr, n));
}
}
// This code is contributed by shikhasingrajput
Javascript
输出:
3
时间复杂度: O(N)
辅助空间: O(1)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。