Sunday, 28 Nov 2021
Category: subset

Given an array arr[] of N integers, the task is to find the maximum count of K, i.e, consecutive integers from 0 to K, that is present in the set S, where S contains all the possible subset-sum values of the array arr[].Examples:Attention reader! Don’t stop learning now. Get hold of all the important DSA […]

#include using namespace std;  int countValidSubsets(int A[], int B[], int N){    int ans = 0;      vector AB(N);              int mx = INT_MIN;                  for (int i = 0; i

Given an array arr[] consisting of N integers and an integer K, the task is to find the minimum number of sets, the array elements can be divided into such that the difference between the maximum and minimum element of each set is at most K.Examples:Input: arr[] = {1, 2, 3, 4, 5}, K = […]

Given an array arr[] consisting of N integers and a positive integer K, the task is to find the sum of all the subsets of size K.Examples:Input: arr[] = {1, 2, 4, 5}, K = 2Output: 36Explanation:The subsets of size K(= 2) are = {1, 2}, {1, 4}, {1, 5}, {2, 4}, {2, 5}, {4, […]

#include using namespace std;  int maxSumAfterPartition(int arr[], int n){        vector pos;          vector neg;          int zero = 0;          int pos_sum = 0;          int neg_sum = 0;          for (int i = 0; i < n; i++) {          if (arr[i] > 0) {              pos.push_back(arr[i]);            pos_sum += arr[i];        }        else if (arr[i] < 0) {              neg.push_back(arr[i]);            neg_sum += arr[i];        }        else {              zero++;        }    }          int ans = 0;              sort(pos.begin(), pos.end());              sort(neg.begin(), neg.end(), greater());              if (pos.size() > 0 && neg.size() > 0) […]

Given an array arr[] consisting of N integers and a positive integer K, the task is to maximize the number of subsets having a product of its size and its maximum element at least K by dividing array element into disjoint subsets.Examples:Input: N = 5, K = 4, arr[] = {1, 5, 4, 2, 3}Output: […]

Count of distinct integers in range [1, N] that do not have any subset sum as KGiven two positive integers N and K such that K≤N, the task is to find the maximum number of distinct integers in the range [1, N] having no subset with a sum equal to K. If there are multiple […]

Count ways to place ‘+’ and ‘-‘ in front of array elements to obtain sum KGiven 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 […]

Count ways to split array into two subsets having difference between their sum equal to KGiven an array A[] of size N and an integer diff, the task is to count the number of ways to split the array into two subsets (non-empty subset is possible) such that the difference between their sums is equal […]

#include using namespace std;  int ans = INT_MAX;  int mini = INT_MAX;  void findClosestTarget(int i, int curr,                       int B[], int M,                       int K){              if (abs(curr – K) < mini) {                  mini = abs(curr - K);                  ans = curr;    }              if (abs(curr - K) == mini) {                  ans = min(ans, curr);    }          if (i >= M)        return;          findClosestTarget(i + 1, curr + B[i],                      B, M, K);          findClosestTarget(i + 1, curr + 2 * […]