Count number of Subsequences in Array in which X and Y are min and max elements
Last Updated :
22 Aug, 2023
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 = 5
Output: 2
Explanation: Subsequences in which the minimum element is X and the maximum element is Y are {2, 5}, {2, 4, 5}.
Input: arr[] = {2, 4, 6, 7, 5, 1, 9, 10, 11}, X = 2, Y = 7
Output: 8
Explanation: subsequences in which the minimum element is X and the maximum element is Y are {2, 4, 6, 7, 5}, {2, 4, 7, 5}, {2, 4, 6, 7}, {2, 4, 7}, {2, 6, 7, 5}, {2, 7, 5}, {2, 6, 7}, {2, 7}
Naive Approach: The basic way to solve the problem is as follows:
The given problem can be solved by generating and counting all possible subsequences of the given array using recursion and checking if each subsequence has the minimum element as X and the maximum element as Y.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int Subsequences(vector< int >& arr, int index,
vector< int >& subarr, int n, int x, int y)
{
if (index == n) {
if (subarr.size() != 0 && subarr[0] == x
&& subarr[subarr.size() - 1] == y) {
return 1;
}
else
return 0;
}
else {
subarr.push_back(arr[index]);
int pic
= Subsequences(arr, index + 1, subarr, n, x, y);
subarr.pop_back();
int notpic
= Subsequences(arr, index + 1, subarr, n, x, y);
return pic + notpic;
}
}
int main()
{
vector< int > arr = { 2, 4, 6, 7, 5, 1, 9, 10, 11 };
int x = 2, y = 7;
vector< int > subarr;
sort(arr.begin(), arr.end());
cout << Subsequences(arr, 0, subarr, arr.size(), x, y);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int
Subsequences(ArrayList<Integer> arr, int index,
ArrayList<Integer> subarr, int n, int x,
int y)
{
if (index == n) {
if (subarr.size() != 0 && subarr.get( 0 ) == x
&& subarr.get(subarr.size() - 1 ) == y) {
return 1 ;
}
else {
return 0 ;
}
}
else {
subarr.add(arr.get(index));
int pic = Subsequences(arr, index + 1 , subarr,
n, x, y);
subarr.remove(subarr.size() - 1 );
int notpic = Subsequences(arr, index + 1 ,
subarr, n, x, y);
return pic + notpic;
}
}
public static void main(String[] args)
{
ArrayList<Integer> arr = new ArrayList<Integer>(
Arrays.asList( 2 , 4 , 6 , 7 , 5 , 1 , 9 , 10 , 11 ));
int x = 2 , y = 7 ;
ArrayList<Integer> subarr
= new ArrayList<Integer>();
Collections.sort(arr);
System.out.println(
Subsequences(arr, 0 , subarr, arr.size(), x, y));
}
}
|
Python3
def Subsequences(arr, index, subarr, n, x, y):
if index = = n:
if subarr and subarr[ 0 ] = = x and subarr[ - 1 ] = = y:
return 1
else :
return 0
else :
subarr.append(arr[index])
pic = Subsequences(arr, index + 1 , subarr, n, x, y)
subarr.pop()
notpic = Subsequences(arr, index + 1 , subarr, n, x, y)
return pic + notpic
if __name__ = = "__main__" :
arr = [ 2 , 4 , 6 , 7 , 5 , 1 , 9 , 10 , 11 ]
x, y = 2 , 7
subarr = []
arr.sort()
print (Subsequences(arr, 0 , subarr, len (arr), x, y))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static int Subsequences(List< int > arr, int index,
List< int > subarr, int n, int x,
int y)
{
if (index == n) {
if (subarr.Count != 0 && subarr[0] == x
&& subarr[subarr.Count - 1] == y) {
return 1;
}
else
return 0;
}
else {
subarr.Add(arr[index]);
int pic = Subsequences(arr, index + 1, subarr,
n, x, y);
subarr.RemoveAt(subarr.Count - 1);
int notpic = Subsequences(arr, index + 1,
subarr, n, x, y);
return pic + notpic;
}
}
static void Main( string [] args)
{
List< int > arr = new List< int >() {
2, 4, 6, 7, 5, 1, 9, 10, 11
};
int x = 2;
int y = 7;
List< int > subarr = new List< int >();
arr.Sort();
Console.WriteLine(Subsequences(arr, 0, subarr,
arr.Count(), x, y));
}
}
|
Javascript
function Subsequences(arr, index, subarr, n, x, y) {
if (index == n) {
if (subarr.length != 0 && subarr[0] == x && subarr[subarr.length - 1] == y) {
return 1;
} else {
return 0;
}
} else {
subarr.push(arr[index]);
let pic = Subsequences(arr, index + 1, subarr, n, x, y);
subarr.pop();
let notpic = Subsequences(arr, index + 1, subarr, n, x, y);
return pic + notpic;
}
}
let arr = [2, 4, 6, 7, 5, 1, 9, 10, 11];
let x = 2, y = 7;
let subarr = [];
arr.sort();
console.log(Subsequences(arr, 0, subarr, arr.length, x, y));
|
Time Complexity: O(2n)
Auxiliary Space: O(n)
Efficient Approach: To solve the problem follow the below idea:
We know that the subsequence will be formed will be in the range (X, Y), and the formula that will come for all the subsequence in the range (X, Y) excluding X and Y will be 2n where n is the number of elements in the range X and Y. Therefore will return the 2n as the answer.
Below are the steps for the above approach:
- Initialize a counter variable say, cnt = 0 to keep track of the number of elements in the range [X, Y].
- Run a loop from i = 0 to i < n and check if the element lies within the range (x, y),
- if (arr[i] > X && arr[i] < Y), increment the cnt variable by 1.
- Return 2cnt, which gives us the number of subsequences, and return 1 << cnt.
Below is the code for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countSubsequences( int arr[], int n, int x, int y)
{
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] > x && arr[i] < y) {
cnt++;
}
}
return (1 << cnt);
}
int main()
{
int arr[] = { 2, 4, 6, 7, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 2, y = 5;
cout << countSubsequences(arr, n, x, y) << endl;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int countSubsequences( int [] arr, int n, int x, int y)
{
int cnt = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] > x && arr[i] < y)
{
cnt++;
}
}
return ( 1 << cnt);
}
public static void main(String[] args) {
int [] arr = { 2 , 4 , 6 , 7 , 5 };
int n = arr.length;
int x = 2 , y = 5 ;
System.out.println(countSubsequences(arr, n, x, y));
}
}
|
Python
def countSubsequences(arr, n, x, y):
cnt = 0
for i in range (n):
if arr[i] > x and arr[i] < y:
cnt + = 1
return 1 << cnt
arr = [ 2 , 4 , 6 , 7 , 5 ]
n = len (arr)
x, y = 2 , 5
print (countSubsequences(arr, n, x, y))
|
C#
using System;
public class GFG {
public static int countSubsequences( int [] arr, int n, int x, int y)
{
int cnt = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] > x && arr[i] < y)
{
cnt++;
}
}
return (1 << cnt);
}
public static void Main() {
int [] arr = {2, 4, 6, 7, 5};
int n = arr.Length;
int x = 2, y = 5;
Console.WriteLine(countSubsequences(arr, n, x, y));
}
}
|
Javascript
function countSubsequences(arr, x, y) {
let cnt = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > x && arr[i] < y) {
cnt++;
}
}
return Math.pow(2, cnt);
}
const arr = [2, 4, 6, 7, 5];
const x = 2, y = 5;
console.log(countSubsequences(arr, x, y));
|
Time Complexity: O(n)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...