总和在 [L, R] 范围内且最大和最小元素之间的差值至少为 X 的子序列计数
给定一个由N个正整数和3 个整数L 、 R和X组成的数组arr[] ,任务是找到大小至少为 2 且总和在[L, R]范围内的子序列的数量,以及它们之间的差最大最小元素至少为X。 (N≤15)
例子:
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
方法:由于N很小,这个问题可以使用位掩码来解决。总共有2 n 个可能的子序列。因此,每个子序列都可以用二进制字符串表示 即掩码,如果第i位被设置,即1 ,则该元素在子序列中被考虑,否则不被考虑。请按照以下步骤解决问题:
- 使用变量i在[0, 2 n – 1]范围内迭代并执行以下步骤:
- 初始化一个变量,例如, cnt为0 , sum为0以存储所选元素的总和。
- 初始化一个变量,比如minVal为INT_MAX和maxVal为INT_MIN以存储子序列中的最小值和最大值。
- 使用变量j在[0, N-1]范围内迭代并执行以下步骤:
- 如果第i 个掩码的第 j位为 on,则将cnt加1,将arr[j]相加并更新maxVal作为maxVal 和 a[j] 的最大值,minVal 作为 minVal和 a[j]的最小值。
- 如果cnt >= 2并且sum在[L, R]范围内并且maxVal和minVal的差大于等于X ,则将ans递增1。
- 完成上述步骤后,打印ans的值作为答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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
输出
2
时间复杂度: O(N×2 N )
辅助空间: O(1)