📌  相关文章
📜  计算包含最大和最小数组元素的子数组

📅  最后修改于: 2021-04-23 19:16:30             🧑  作者: Mango

给定一个由N个不同的整数组成的数组arr [] ,任务是查找既包含给定数组的最大元素又包含最小元素的子数组的数量。

例子:

天真的方法:最简单的方法是首先遍历数组,找到数组的最大值和最小值,然后生成给定数组的所有可能的子数组。对于每个子数组,检查它是否同时包含最大最小数组元素。对于所有此类子阵列,将计数增加1。最后,打印此类子阵列的计数。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:请按照以下步骤优化上述方法:

  • 查找最大最小元素的索引。令ij为各自的索引,使i
  • 所有从索引开始到i并在j之后的索引结束的子数组都将包含最大和最小数组元素。
  • 因此,子数组起始索引的可能索引为[0,i] (总数= i + 1 )。
  • 因此,子数组结尾索引的可能索引为[j,N – 1] (总数= 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


输出:
3







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