Count of subsequences with a sum in range [L, R] and difference between max and min element at least X
Last Updated :
13 Jul, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the number of subsequences
// of the given array with a sum in range [L, R]
// and the difference between the maximum and
// minimum element is at least X
int numberofSubsequences(int a[], int L,
int R, int X, int n)
{
// Initialize answer as 0
int ans = 0;
// Creating mask from [0, 2^n-1]
for (int i = 0; i < (1 << n); i++) {
// Stores the count and sum of
// selected elements respectively
int cnt = 0, sum = 0;
// Variables to store the value of
// Minimum and maximum element
int minVal = INT_MAX, maxVal = INT_MIN;
// Traverse the array
for (int j = 0; j < n; j++) {
// If the jth bit of the ith
// mask is on
if ((i & (1 << j))) {
cnt += 1;
// Add the selected element
sum += a[j];
// Update maxVal and minVal value
maxVal = max(maxVal, a[j]);
minVal = min(minVal, a[j]);
}
}
// Check if the given conditions are
// true, increment ans by 1.
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1;
}
}
return ans;
}
// Driver Code
int main()
{
// Given Input
int a[] = { 10, 20, 30, 25 };
int L = 40, R = 50, X = 10;
int N = sizeof(a) / sizeof(a[0]);
// Function Call
cout << numberofSubsequences(a, L, R, X, N)
<< endl;
return 0;
}
Java
// Java program for the above approach
public class GFG {
// Function to find the number of subsequences
// of the given array with a sum in range [L, R]
// and the difference between the maximum and
// minimum element is at least X
static int numberofSubsequences(int a[], int L, int R,
int X, int n)
{
// Initialize answer as 0
int ans = 0;
// Creating mask from [0, 2^n-1]
for (int i = 0; i < (1 << n); i++) {
// Stores the count and sum of
// selected elements respectively
int cnt = 0, sum = 0;
// Variables to store the value of
// Minimum and maximum element
int minVal = Integer.MAX_VALUE,
maxVal = Integer.MIN_VALUE;
// Traverse the array
for (int j = 0; j < n; j++) {
// If the jth bit of the ith
// mask is on
if ((i & (1 << j)) == 0) {
cnt += 1;
// Add the selected element
sum += a[j];
// Update maxVal and minVal value
maxVal = Math.max(maxVal, a[j]);
minVal = Math.min(minVal, a[j]);
}
}
// Check if the given conditions are
// true, increment ans by 1.
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1;
}
}
return ans;
}
// Driver code
public static void main(String[] args)
{
int a[] = { 10, 20, 30, 25 };
int L = 40, R = 50, X = 10;
int N = a.length;
// Function Call
System.out.println(
numberofSubsequences(a, L, R, X, N));
}
}
// This code is contributed by abhinavjain194
Python3
# Python3 program for the above approach
import sys
# Function to find the number of subsequences
# of the given array with a sum in range [L, R]
# and the difference between the maximum and
# minimum element is at least X
def numberofSubsequences(a, L, R, X, n):
# Initialize answer as 0
ans = 0
# Creating mask from [0, 2^n-1]
for i in range(0, (1 << n), 1):
# Stores the count and sum of
# selected elements respectively
cnt = 0
sum = 0
# Variables to store the value of
# Minimum and maximum element
minVal = sys.maxsize
maxVal = -sys.maxsize - 1
# Traverse the array
for j in range(n):
# If the jth bit of the ith
# mask is on
if ((i & (1 << j))):
cnt += 1
# Add the selected element
sum += a[j]
# Update maxVal and minVal value
maxVal = max(maxVal, a[j])
minVal = min(minVal, a[j])
# Check if the given conditions are
# true, increment ans by 1.
if (cnt >= 2 and sum >= L and
sum <= R and (maxVal - minVal >= X)):
ans += 1
return ans
# Driver Code
if __name__ == '__main__':
# Given Input
a = [ 10, 20, 30, 25 ]
L = 40
R = 50
X = 10
N = len(a)
# Function Call
print(numberofSubsequences(a, L, R, X, N))
# This code is contributed by bgangwar59
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the number of subsequences
// of the given array with a sum in range [L, R]
// and the difference between the maximum and
// minimum element is at least X
static int numberofSubsequences(int[] a, int L, int R,
int X, int n)
{
// Initialize answer as 0
int ans = 0;
// Creating mask from [0, 2^n-1]
for(int i = 0; i < (1 << n); i++)
{
// Stores the count and sum of
// selected elements respectively
int cnt = 0, sum = 0;
// Variables to store the value of
// Minimum and maximum element
int minVal = Int32.MaxValue,
maxVal = Int32.MinValue;
// Traverse the array
for(int j = 0; j < n; j++)
{
// If the jth bit of the ith
// mask is on
if ((i & (1 << j)) == 0)
{
cnt += 1;
// Add the selected element
sum += a[j];
// Update maxVal and minVal value
maxVal = Math.Max(maxVal, a[j]);
minVal = Math.Min(minVal, a[j]);
}
}
// Check if the given conditions are
// true, increment ans by 1.
if (cnt >= 2 && sum >= L && sum <= R &&
(maxVal - minVal >= X))
{
ans += 1;
}
}
return ans;
}
// Driver Code
static public void Main()
{
// Given input
int[] a = { 10, 20, 30, 25 };
int L = 40, R = 50, X = 10;
int N = a.Length;
// Function Call
Console.Write(numberofSubsequences(a, L, R, X, N));
}
}
// This code is contributed by avijitmondal1998
JavaScript
<script>
// Javascript program for the above approach
// Function to find the number of subsequences
// of the given array with a sum in range [L, R]
// and the difference between the maximum and
// minimum element is at least X
function numberofSubsequences(a, L, R, X, n)
{
// Initialize answer as 0
let ans = 0;
// Creating mask from [0, 2^n-1]
for (let i = 0; i < (1 << n); i++) {
// Stores the count and sum of
// selected elements respectively
let cnt = 0, sum = 0;
// Variables to store the value of
// Minimum and maximum element
let minVal = Number.MAX_SAFE_INTEGER, maxVal = Number.MIN_SAFE_INTEGER;
// Traverse the array
for (let j = 0; j < n; j++)
{
// If the jth bit of the ith
// mask is on
if ((i & (1 << j)))
{
cnt += 1;
// Add the selected element
sum += a[j];
// Update maxVal and minVal value
maxVal = Math.max(maxVal, a[j]);
minVal = Math.min(minVal, a[j]);
}
}
// Check if the given conditions are
// true, increment ans by 1.
if (cnt >= 2 && sum >= L && sum <= R
&& (maxVal - minVal >= X)) {
ans += 1;
}
}
return ans;
}
// Driver Code
// Given Input
let a = [10, 20, 30, 25];
let L = 40, R = 50, X = 10;
let N = a.length;
// Function Call
document.write(numberofSubsequences(a, L, R, X, N) + "<br>");
// This code is contributed by gfgking.
</script>
Time Complexity: O(N×2N)
Auxiliary Space: O(1)
Similar Reads
Cost of creating smallest subsequence with sum of difference between adjacent elements maximum Given two array of N integers arr[] and costArray[], representing removal cost associated with each element. The task is to find the subsequence from the given array of minimum cost such that the sum of the difference between adjacent elements is maximum. On removing each element, cost is incurred.E
10 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
Maximum subset sum having difference between its maximum and minimum in range [L, R] Given an array arr[] of N positive integers and a range [L, R], the task is to find the maximum subset-sum such that the difference between the maximum and minimum elements of the subset lies in the given range. Examples: Input: arr[] = {6, 5, 0, 9, 1}, L = 0, R = 3Output: 15Explanation: The subset
9 min read
Count of subsequences in an array with sum less than or equal to X Given an integer array arr[] of size N and an integer X, the task is to count the number of subsequences in that array such that its sum is less than or equal to X. Note: 1 <= N <= 1000 and 1 <= X <= 1000, where N is the size of the array. Examples: Input : arr[] = {84, 87, 73}, X = 100
13 min read
Sum of width (max and min diff) of all Subsequences Given an array A[] of integers. The task is to return the sum of the width of all subsequences of A. For any sequence S, the width of S is the difference between the maximum and minimum elements of S. Note: Since the answer can be large, print the answer modulo 10^9 + 7. Examples: Input : A[] = {1,
5 min read
Count distinct elements from a range of a sorted sequence from a given frequency array Given two integers L and R and an array arr[] consisting of N positive integers( 1-based indexing ) such that the frequency of ith element of a sorted sequence, say A[], is arr[i]. The task is to find the number of distinct elements from the range [L, R] in the sequence A[]. Examples: Input: arr[] =
13 min read