Count of subsequences with a sum in range [L, R] and difference between max and min element at least X
Given an array arr[] consisting of N positive integers and 3 integers L, R, and X, the task is to find the number of subsequences of size atleast 2 with a sum in the range [L, R], and the difference between the maximum and minimum element is at least X. (N≤15)
Examples:
Input: arr[] = {1 2 3}, L = 5, R = 6, X = 1
Output: 2
Explanation:
There are two subsequences possible i.e. {2, 3} and {1, 2, 3}.
Input: arr[] = {10, 20, 30, 25}, L = 40, R = 50, X = 10
Output: 2
Approach: Since N is small, this problem can be solved using bitmasking. There are total 2n subsequences possible. So, every subsequence can be represented by a binary string i.e. mask where if the ith bit is set i.e 1 then the element is considered in the subsequences otherwise not. Follow the steps below to solve the problem:
- Iterate in the range [0, 2n – 1] using the variable i and perform the following steps:
- Initialize a variable say, cnt as 0 and sum as 0 to store the sum of selected elements.
- Initialize a variable say, minVal as INT_MAX and maxVal as INT_MIN to store minimum value and maximum value in the subsequence.
- Iterate in the range [0, N-1] using the variable j and perform the following steps:
- If the jth bit of the ith mask is on, then Increment cnt by 1, add arr[j] to sum and update maxVal as the maximum of maxVal and a[j] and minVal as the minimum of minVal and a[j].
- If cnt >= 2 and sum is in the range [L, R] and the difference of maxVal and minVal is greater than equal to X, then increment ans by 1.
- After completing the above steps, print the value of ans as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int numberofSubsequences( int a[], int L,
int R, int X, int n)
{
int ans = 0;
for ( int i = 0; i < (1 << n); i++) {
int cnt = 0, sum = 0;
int minVal = INT_MAX, maxVal = INT_MIN;
for ( int j = 0; j < n; j++) {
if ((i & (1 << j))) {
cnt += 1;
sum += a[j];
maxVal = max(maxVal, a[j]);
minVal = min(minVal, a[j]);
}
}
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1;
}
}
return ans;
}
int main()
{
int a[] = { 10, 20, 30, 25 };
int L = 40, R = 50, X = 10;
int N = sizeof (a) / sizeof (a[0]);
cout << numberofSubsequences(a, L, R, X, N)
<< endl;
return 0;
}
|
Java
public class GFG {
static int numberofSubsequences( int a[], int L, int R,
int X, int n)
{
int ans = 0 ;
for ( int i = 0 ; i < ( 1 << n); i++) {
int cnt = 0 , sum = 0 ;
int minVal = Integer.MAX_VALUE,
maxVal = Integer.MIN_VALUE;
for ( int j = 0 ; j < n; j++) {
if ((i & ( 1 << j)) == 0 ) {
cnt += 1 ;
sum += a[j];
maxVal = Math.max(maxVal, a[j]);
minVal = Math.min(minVal, a[j]);
}
}
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1 ;
}
}
return ans;
}
public static void main(String[] args)
{
int a[] = { 10 , 20 , 30 , 25 };
int L = 40 , R = 50 , X = 10 ;
int N = a.length;
System.out.println(
numberofSubsequences(a, L, R, X, N));
}
}
|
Python3
import sys
def numberofSubsequences(a, L, R, X, n):
ans = 0
for i in range ( 0 , ( 1 << n), 1 ):
cnt = 0
sum = 0
minVal = sys.maxsize
maxVal = - sys.maxsize - 1
for j in range (n):
if ((i & ( 1 << j))):
cnt + = 1
sum + = a[j]
maxVal = max (maxVal, a[j])
minVal = min (minVal, a[j])
if (cnt > = 2 and sum > = L and
sum < = R and (maxVal - minVal > = X)):
ans + = 1
return ans
if __name__ = = '__main__' :
a = [ 10 , 20 , 30 , 25 ]
L = 40
R = 50
X = 10
N = len (a)
print (numberofSubsequences(a, L, R, X, N))
|
C#
using System;
class GFG{
static int numberofSubsequences( int [] a, int L, int R,
int X, int n)
{
int ans = 0;
for ( int i = 0; i < (1 << n); i++)
{
int cnt = 0, sum = 0;
int minVal = Int32.MaxValue,
maxVal = Int32.MinValue;
for ( int j = 0; j < n; j++)
{
if ((i & (1 << j)) == 0)
{
cnt += 1;
sum += a[j];
maxVal = Math.Max(maxVal, a[j]);
minVal = Math.Min(minVal, a[j]);
}
}
if (cnt >= 2 && sum >= L && sum <= R &&
(maxVal - minVal >= X))
{
ans += 1;
}
}
return ans;
}
static public void Main()
{
int [] a = { 10, 20, 30, 25 };
int L = 40, R = 50, X = 10;
int N = a.Length;
Console.Write(numberofSubsequences(a, L, R, X, N));
}
}
|
Javascript
<script>
function numberofSubsequences(a, L, R, X, n)
{
let ans = 0;
for (let i = 0; i < (1 << n); i++) {
let cnt = 0, sum = 0;
let minVal = Number.MAX_SAFE_INTEGER, maxVal = Number.MIN_SAFE_INTEGER;
for (let j = 0; j < n; j++)
{
if ((i & (1 << j)))
{
cnt += 1;
sum += a[j];
maxVal = Math.max(maxVal, a[j]);
minVal = Math.min(minVal, a[j]);
}
}
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1;
}
}
return ans;
}
let a = [10, 20, 30, 25];
let L = 40, R = 50, X = 10;
let N = a.length;
document.write(numberofSubsequences(a, L, R, X, N) + "<br>" );
</script>
|
Time Complexity: O(N×2N)
Auxiliary Space: O(1)
Last Updated :
13 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...