给定一个由N个整数组成的数组arr [] ,任务是查找子序列的数量,这些子序列包含给定数组中存在的最大和最小元素。
例子 :
Input: arr[] = {1, 2, 3, 4}
Output: 4
Explanation:
There are 4 subsequence {1, 4}, {1, 2, 4}, {1, 3, 4}, {1, 2, 3, 4} which contains the maximum array element(= 4) and the minimum array element(= 1).
Input: arr[] = {4, 4, 4, 4}
Output: 15
天真的方法:最简单的方法是首先遍历数组,找到数组的最大值和最小值,然后生成给定数组的所有可能的子序列。对于每个子序列,检查它是否同时包含最大和最小数组元素。对于所有此类子序列,将计数增加1。最后,打印此类子序列的计数。
时间复杂度: O(2 N )
辅助空间: O(N)
高效方法:请按照以下步骤优化上述方法:
- 查找最大元素和最小元素的出现次数。令i和j为各自的计数。
- 检查最大和最小元素是否相同。如果发现为真,则可能的子序列是数组的所有非空子序列。
- 否则,为了满足子序列的条件,它应包含i的至少1个元素和j的至少1个元素。因此,所需的子序列数由以下公式给出:
(pow(2, i) -1 ) * ( pow(2, j) -1 ) * pow(2, n-i-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
输出:
4
时间复杂度: O(N)
辅助空间: O(1)