- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count subsets that satisfy the given condition in C++
Given an array of numbers and an integer x as input. The goal is to find all the subsets of arr[] such that individual elements of that set as well as the sum of them fully divisible by x.
For Example
Input
arr[] = {1,2,3,4,5,6} x=3
Output
Count of subsets that satisfy the given condition :3
Explanation
The subsets will be: [3], [6], [3,6]
Input
arr[] = {1,2,3,4,5,6} x=4
Output
Count of subsets that satisfy the given condition :1
Explanation
The subsets will be: [4]
Approach used in the below program is as follows −
In this approach we will count the elements of arr[] that are fully divisible by x and then return 2count−1 as required number of subsets.
Take an integer array arr[].
Take x as input.
Function count(int arr[], int n, int x) takes an array and x and returns count of subsets that satisfy the given condition.
If x is 1 then it divides all elements, so return
Example
#include <bits/stdc++.h> #define ll long long int using namespace std; int sub_sets(int arr[], int size, int val){ int count = 0; if (val == 1){ count = pow(2, size) − 1; return count; } for (int i = 0; i < size; i++){ if (arr[i] % val == 0){ count++; } } count = pow(2, count) − 1; return count; } int main(){ int arr[] = { 4, 6, 1, 3, 8, 10, 12 }, val = 4; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Count of sub−sets that satisfy the given condition are: "<<sub_sets(arr, size, val); return 0; }
Output
If we run the above code it will generate the following output −
Count of sub−sets that satisfy the given condition are: 7
- Related Articles
- Count all possible N digit numbers that satisfy the given condition in C++
- Count index pairs which satisfy the given condition in C++
- Find numbers a and b that satisfy the given condition in C++
- Count triplet pairs (A, B, C) of points in 2-D space that satisfy the given condition in C++
- How to count the number of values that satisfy a condition in an R vector?
- C++ program to find out the number of pairs in an array that satisfy a given condition
- Program to find number of subsequences that satisfy the given sum condition using Python
- Find n positive integers that satisfy the given equations in C++
- Count subsets having distinct even numbers in C++
- Program to count subsets that sum up to k in python
- Maximum size of sub-array that satisfies the given condition in C++
- Count number of subsets of a set with GCD equal to a given number in C++
- Maximum size of sub-array that satisfies the given condition in C++ program
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++

Advertisements