Count subsequences which contains both the maximum and minimum array element
Last Updated :
07 May, 2021
Given an array arr[] consisting of N integers, the task is to find the number of subsequences which contain the maximum as well as the minimum element present in the given array.
Example :
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
Naive Approach: The simplest approach is to first, traverse the array and find the maximum and minimum of the array and then generate all possible subsequences of the given array. For each subsequence, check if it contains both the maximum and the minimum array element. For all such subsequences, increase the count by 1. Finally, print the count of such subsequences.
Time Complexity: O(2N)
Auxiliary Space: O(N)
Efficient Approach: Follow the steps below to optimize the above approach:
- Find the count of occurrences of the maximum element and the minimum element. Let i and j be the respective count.
- Check if the maximum and the minimum element are the same or not. If found to be true, then the possible subsequences are all non-empty subsequences of the array.
- Otherwise, for satisfying the condition of the subsequence, it should contain at least 1 element from i and at least 1 element from j. Therefore, the required count of subsequences is given by the following equation:
(pow(2, i) -1 ) * ( pow(2, j) -1 ) * pow(2, n-i-j)
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include<bits/stdc++.h>
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
<script>
// JavaScript program for the above approach
// Function to calculate the
// count of subsequences
function countSubSequence(arr, n)
{
// Find the maximum
// from the array
let maximum = Math.max(...arr);
// Find the minimum
// from the array
let minimum = Math.min(...arr);
// If array contains only
// one distinct element
if (maximum == minimum)
return Math.pow(2, n) - 1;
// Find the count of maximum
let i = count(arr, maximum);
// Find the count of minimum
let j = count(arr, minimum);
// Finding the result
// with given condition
let res = (Math.pow(2, i) - 1) *
(Math.pow(2, j) - 1) *
Math.pow(2, n - i - j);
return res;
}
function count(arr, value)
{
let sum = 0;
for(let i = 0; i < arr.length; i++)
if (arr[i] == value)
sum++;
return sum;
}
// Driver Code
let arr = [ 1, 2, 3, 4 ];
let n = arr.length;
// Function call
document.write(countSubSequence(arr, n));
// This code is contributed by souravghosh0416.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Count subarrays which contains both the maximum and minimum array element Given an array arr[] consisting of N distinct integers, the task is to find the number of subarrays which contains both the maximum and the minimum element from the given array. Examples: Input: arr[] = {1, 2, 3, 4}Output: 1 Explanation: Only a single subarray {1, 2, 3, 4} consists of both the maxim
11 min read
Count subsequences for every array element in which they are the maximum Given an array arr[] consisting of N unique elements, the task is to generate an array B[] of length N such that B[i] is the number of subsequences in which arr[i] is the maximum element. Examples: Input: arr[] = {2, 3, 1}Output: {2, 4, 1}Explanation: Subsequences in which arr[0] ( = 2) is maximum a
11 min read
Count number of Subsequences in Array in which X and Y are min and max elements Given an array arr[] consisting of N unique elements, the task is to return the count of the subsequences in an array in which the minimum element is X and the maximum element is Y. Examples: Input: arr[] = {2, 4, 6, 7, 5}, X = 2, Y = 5Output: 2Explanation: Subsequences in which the minimum element
11 min read
Count subarrays for every array element in which they are the minimum Given an array arr[] consisting of N integers, the task is to create an array brr[] of size N where brr[i] represents the count of subarrays in which arr[i] is the smallest element. Examples: Input: arr[] = {3, 2, 4} Output: {1, 3, 1} Explanation: For arr[0], there is only one subarray in which 3 is
15+ min read
Maximize subarrays count containing the maximum and minimum Array element after deleting at most one element Given an array arr[] of size N. The task is to maximize the count of subarrays that contain both the minimum and maximum elements of the array by deleting at most one element from the array. Examples: Input: arr[] = {7, 2, 5, 4, 3, 1} Output: 4 Explanation: Delete 1 from the array then resultant arr
11 min read