📌  相关文章
📜  最小和最大元素之和小于 K 的子集计数

📅  最后修改于: 2021-10-26 05:31:06             🧑  作者: Mango

给定一个整数数组arr[]和一个整数K ,任务是找到非空子集 S 的数量,使得min(S) + max(S) < K
例子:

方法

  1. 首先对输入数组进行排序。
  2. 现在使用双指针技术来计算子集的数量。
  3. 取左右两个指针并设置 left = 0 和 right = N-1。
  1. 重复下面的过程直到left <= right

下面是上述方法的实现:

C++
// C++ program to print count
// of subsets S such that
// min(S) + max(S) < K
 
#include 
using namespace std;
 
// Function that return the
// count of subset such that
// min(S) + max(S) < K
int get_subset_count(int arr[], int K,
                     int N)
{
    // Sorting the array
    sort(arr, arr + N);
 
    int left, right;
    left = 0;
    right = N - 1;
 
    // ans stores total number of subsets
    int ans = 0;
 
    while (left <= right) {
        if (arr[left] + arr[right] < K) {
 
            // add all possible subsets
            // between i and j
            ans += 1 << (right - left);
            left++;
        }
        else {
            // Decrease the sum
            right--;
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 5, 7 };
    int K = 8;
    int N = sizeof(arr) / sizeof(arr[0]);
    cout << get_subset_count(arr, K, N);
    return 0;
}


Java
// Java program to print count
// of subsets S such that
// Math.min(S) + Math.max(S) < K
import java.util.*;
 
class GFG{
 
// Function that return the
// count of subset such that
// Math.min(S) + Math.max(S) < K
static int get_subset_count(int arr[], int K,
                                       int N)
{
     
    // Sorting the array
    Arrays.sort(arr);
 
    int left, right;
    left = 0;
    right = N - 1;
 
    // ans stores total number
    // of subsets
    int ans = 0;
 
    while (left <= right)
    {
        if (arr[left] + arr[right] < K)
        {
 
            // Add all possible subsets
            // between i and j
            ans += 1 << (right - left);
            left++;
        }
        else
        {
             
            // Decrease the sum
            right--;
        }
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 2, 4, 5, 7 };
    int K = 8;
    int N = arr.length;
     
    System.out.print(get_subset_count(arr, K, N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to print
# count of subsets S such
# that min(S) + max(S) < K
 
# Function that return the
# count of subset such that
# min(S) + max(S) < K
def get_subset_count(arr, K, N):
 
    # Sorting the array
    arr.sort()
 
    left = 0;
    right = N - 1;
 
    # ans stores total number of subsets
    ans = 0;
 
    while (left <= right):
        if (arr[left] + arr[right] < K):
             
            # Add all possible subsets
            # between i and j
            ans += 1 << (right - left);
            left += 1;
        else:
             
            # Decrease the sum
            right -= 1;
     
    return ans;
 
# Driver code
arr = [ 2, 4, 5, 7 ];
K = 8;
 
print(get_subset_count(arr, K, 4))
 
# This code is contributed by grand_master


C#
// C# program to print count
// of subsets S such that
// Math.Min(S) + Math.Max(S) < K
using System;
 
class GFG{
 
// Function that return the
// count of subset such that
// Math.Min(S) + Math.Max(S) < K
static int get_subset_count(int []arr, int K,
                                       int N)
{
     
    // Sorting the array
    Array.Sort(arr);
 
    int left, right;
    left = 0;
    right = N - 1;
 
    // ans stores total number
    // of subsets
    int ans = 0;
 
    while (left <= right)
    {
        if (arr[left] + arr[right] < K)
        {
             
            // Add all possible subsets
            // between i and j
            ans += 1 << (right - left);
            left++;
        }
        else
        {
             
            // Decrease the sum
            right--;
        }
    }
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 2, 4, 5, 7 };
    int K = 8;
    int N = arr.Length;
     
    Console.Write(get_subset_count(arr, K, N));
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
4

时间复杂度: O(N* log N)
辅助空间: O(1)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程