Count of subsets having sum of min and max element less than K
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++
#include <bits/stdc++.h>
using namespace std;
int get_subset_count( int arr[], int K,
int N)
{
sort(arr, arr + N);
int left, right;
left = 0;
right = N - 1;
int ans = 0;
while (left <= right) {
if (arr[left] + arr[right] < K) {
ans += 1 << (right - left);
left++;
}
else {
right--;
}
}
return ans;
}
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
import java.util.*;
class GFG{
static int get_subset_count( int arr[], int K,
int N)
{
Arrays.sort(arr);
int left, right;
left = 0 ;
right = N - 1 ;
int ans = 0 ;
while (left <= right)
{
if (arr[left] + arr[right] < K)
{
ans += 1 << (right - left);
left++;
}
else
{
right--;
}
}
return ans;
}
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));
}
}
|
Python3
def get_subset_count(arr, K, N):
arr.sort()
left = 0 ;
right = N - 1 ;
ans = 0 ;
while (left < = right):
if (arr[left] + arr[right] < K):
ans + = 1 << (right - left);
left + = 1 ;
else :
right - = 1 ;
return ans;
arr = [ 2 , 4 , 5 , 7 ];
K = 8 ;
print (get_subset_count(arr, K, 4 ))
|
C#
using System;
class GFG{
static int get_subset_count( int []arr, int K,
int N)
{
Array.Sort(arr);
int left, right;
left = 0;
right = N - 1;
int ans = 0;
while (left <= right)
{
if (arr[left] + arr[right] < K)
{
ans += 1 << (right - left);
left++;
}
else
{
right--;
}
}
return ans;
}
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));
}
}
|
Javascript
<script>
function get_subset_count(arr,K,N)
{
(arr).sort( function (a,b){ return a-b;});
let left, right;
left = 0;
right = N - 1;
let ans = 0;
while (left <= right)
{
if (arr[left] + arr[right] < K)
{
ans += 1 << (right - left);
left++;
}
else
{
right--;
}
}
return ans;
}
let arr=[ 2, 4, 5, 7];
let K = 8;
let N = arr.length;
document.write(get_subset_count(arr, K, N));
</script>
|
Time Complexity: O(N* log N)
Auxiliary Space: O(1)
Last Updated :
16 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...