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

📅  最后修改于: 2021-09-06 05:43:18             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到包含给定数组中存在的最大值和最小值的子序列的数量。

例子 :

朴素的方法:最简单的方法是首先遍历数组并找到数组的最大值和最小值,然后生成给定数组的所有可能子序列。对于每个子序列,检查它是否同时包含最大最小数组元素。对于所有此类子序列,将计数增加 1。最后,打印此类子序列的计数。

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

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

  • 找出最大元素和最小元素出现的次数。让 i 和 j 分别为计数。
  • 检查最大和最小元素是否相同。如果发现为真,则可能的子序列都是数组的非空子序列。
  • 否则,为了满足子序列的条件,它应该包含至少 1 个来自i 的元素和至少 1 个来自j 的元素。因此,所需的子序列数由以下等式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach
#include
using namespace std;
 
int count(int arr[], int n, int value);
 
// Function to calculate the
// count of subsequences
double countSubSequence(int arr[], int n)
{
   
    // Find the maximum
    // from the array
    int maximum = *max_element(arr, arr + n);
  
    // Find the minimum
    // from the array
    int minimum = *min_element(arr, arr + n);
  
    // If array contains only
    // one distinct element
    if (maximum == minimum)
        return pow(2, n) - 1;
  
    // Find the count of maximum
    int i = count(arr, n, maximum);
  
    // Find the count of minimum
    int j = count(arr, n, minimum);
  
    // Finding the result
    // with given condition
    double res = (pow(2, i) - 1) *
                 (pow(2, j) - 1) *
                  pow(2, n - i - j);
   
    return res;
}
  
int count(int arr[], int n, int value)
{
    int sum = 0;
      
    for(int i = 0; i < n; i++)
        if (arr[i] == value)
            sum++;
              
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << countSubSequence(arr, n) << endl;
}
 
// This code is contributed by rutvik_56


Java
// Java program for the above approach
import java.util.Arrays;
 
class GFG{
     
// Function to calculate the
// count of subsequences
static double countSubSequence(int[] arr, int n)
{
     
    // Find the maximum
    // from the array
    int maximum = Arrays.stream(arr).max().getAsInt();
 
    // Find the minimum
    // from the array
    int minimum = Arrays.stream(arr).min().getAsInt();
 
    // If array contains only
    // one distinct element
    if (maximum == minimum)
        return Math.pow(2, n) - 1;
 
    // Find the count of maximum
    int i = count(arr, maximum);
 
    // Find the count of minimum
    int j = count(arr, minimum);
 
    // Finding the result
    // with given condition
    double res = (Math.pow(2, i) - 1) *
                 (Math.pow(2, j) - 1) *
                  Math.pow(2, n - i - j);
 
    return res;
}
 
static int count(int[] arr, int value)
{
    int sum = 0;
     
    for(int i = 0; i < arr.length; i++)
        if (arr[i] == value)
            sum++;
             
    return sum;
}
 
// Driver Code
public static void main(String[] args)
{
    int[] arr = { 1, 2, 3, 4 };
    int n = arr.length;
 
    // Function call
    System.out.println(countSubSequence(arr, n));
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
 
# Function to calculate the
# count of subsequences
def countSubSequence(arr, n):
 
    # Find the maximum
    # from the array
    maximum = max(arr)
 
    # Find the minimum
    # from the array
    minimum = min(arr)
 
    # If array contains only
    # one distinct element
    if maximum == minimum:
        return pow(2, n)-1
 
    # Find the count of maximum
    i = arr.count(maximum)
 
    # Find the count of minimum
    j = arr.count(minimum)
 
    # Finding the result
    # with given condition
    res = (pow(2, i) - 1) * (pow(2, j) - 1) * pow(2, n-i-j)
 
    return res
 
 
# Driver Code
arr = [1, 2, 3, 4]
n = len(arr)
 
# Function call
print(countSubSequence(arr, n))


C#
// C# program for
// the above approach
using System;
using System.Linq;
class GFG{
     
// Function to calculate the
// count of subsequences
static double countSubSequence(int[] arr,
                               int n)
{   
  // Find the maximum
  // from the array
  int maximum = arr.Max();
 
  // Find the minimum
  // from the array
  int minimum = arr.Min();
 
  // If array contains only
  // one distinct element
  if (maximum == minimum)
    return Math.Pow(2, n) - 1;
 
  // Find the count of maximum
  int i = count(arr, maximum);
 
  // Find the count of minimum
  int j = count(arr, minimum);
 
  // Finding the result
  // with given condition
  double res = (Math.Pow(2, i) - 1) *
               (Math.Pow(2, j) - 1) *
                Math.Pow(2, n - i - j);
  return res;
}
 
static int count(int[] arr, int value)
{
  int sum = 0;
 
  for(int i = 0; i < arr.Length; i++)
    if (arr[i] == value)
      sum++;
 
  return sum;
}
 
// Driver Code
public static void Main(String[] args)
{
  int[] arr = {1, 2, 3, 4};
  int n = arr.Length;
 
  // Function call
  Console.WriteLine(countSubSequence(arr, n));
}
}
  
// This code is contributed by shikhasingrajput


Javascript


输出:
4

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