Count of subsets having sum of min and max element less than K
Last Updated :
16 Jan, 2022
Given an integer array arr[] and an integer K, the task is to find the number of non-empty subsets S such that min(S) + max(S) < K.
Examples:
Input: arr[] = {2, 4, 5, 7} K = 8
Output: 4
Explanation:
The possible subsets are {2}, {2, 4}, {2, 4, 5} and {2, 5}
Input:: arr[] = {2, 4, 2, 5, 7} K = 10
Output: 26
Approach
- Sort the input array first.
- Now use Two Pointer Technique to count the number of subsets.
- Let take two pointers left and right and set left = 0 and right = N-1.
if (arr[left] + arr[right] < K )
Increment the left pointer by 1 and add 2 j - i into answer, because the left and right values make up a potential end values of a subset. All the values from [i, j - 1] also make up end of subsets which will have the sum < K. So, we need to calculate all the possible subsets for left = i and right ? [i, j]. So, after summing up values 2 j - i + 1 + 2 j - i - 2 + ... + 2 0 of the GP, we get 2 j - i .
if( arr[left] + arr[right] >= K )
Decrement the right pointer by 1.
- Repeat the below process until left <= right.
Below is the implementation of the above approach:
C++
// C++ program to print count
// of subsets S such that
// min(S) + max(S) < K
#include <bits/stdc++.h>
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
<script>
// JavaScript program to print count
// of subsets S such that
// Math.min(S) + Math.max(S) < K
// Function that return the
// count of subset such that
// Math.min(S) + Math.max(S) < K
function get_subset_count(arr,K,N)
{
// Sorting the array
(arr).sort(function(a,b){return a-b;});
let left, right;
left = 0;
right = N - 1;
// ans stores total number
// of subsets
let 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
let arr=[ 2, 4, 5, 7];
let K = 8;
let N = arr.length;
document.write(get_subset_count(arr, K, N));
// This code is contributed by patel2127
</script>
Time Complexity: O(N* log N)
Auxiliary Space: O(1)
Similar Reads
Count of Arrays of size N with elements in range [0, (2^K)-1] having maximum sum & bitwise AND 0 Given two integers N and K, The task is to find the count of all possible arrays of size N with maximum sum & bitwise AND of all elements as 0. Also, elements should be within the range of 0 to 2K-1. Examples: Input: N = 3, K = 2Output: 9Explanation: 22 - 1 = 3, so elements of arrays should be b
6 min read
Count ways to place '+' and '-' in front of array elements to obtain sum K Given an array A[] consisting of N non-negative integers, and an integer K, the task is to find the number of ways '+' and '-' operators can be placed in front of elements of the array A[] such that the sum of the array becomes K. Examples: Input: A[] = {1, 1, 2, 3}, N = 4, K = 1Output: 3Explanation
10 min read
Count of subarrays of size K which is a permutation of numbers from 1 to K Given an array arr of distinct integers, the task is to find the count of sub-arrays of size i having all elements from 1 to i, in other words, the sub-array is any permutation of elements from 1 to i, with 1 < = i <= N. Examples: Input: arr[] = {2, 3, 1, 5, 4} Output: 3 Explanation: we have {
6 min read
Find all possible values of K such that the sum of first N numbers starting from K is G Given a positive integer G, the task is to find the number of values of K such that the sum of the first N numbers starting from K is G i.e., (K + (K + 1) + ... + (K + N - 1)) = G, where N can be any positive integer. Examples: Input: G = 10Output: 2Explanation:Following are the possible values of K
9 min read
Maximum and minimum count of elements with sum at most K Given an array arr[] of size N and an integer K, the task is to find the maximum and minimum number of elements whose sum is less than equal to K. Examples: Input: N = 4, arr[ ] = {6, 2, 1, 3}, K = 7Output: 3 1Explanation:Maximum number of elements whose sum is less than equal to K is 3 i.e [1, 2, 3
7 min read