Count sub-sets that satisfy the given condition
Last Updated :
12 Sep, 2022
Given an array arr[] and an integer x, the task is to count the number of sub-sets of arr[] sum of all of whose sub-sets (individually) is divisible by x.
Examples:
Input: arr[] = {2, 4, 3, 7}, x = 2
Output: 3
All valid sub-sets are {2}, {4} and {2, 4}
{2} => 2 is divisible by 2
{4} => 4 is divisible by 2
{2, 4} => 2, 4 and 6 are all divisible by 2
Input: arr[] = {2, 3, 4, 5}, x = 1
Output: 15
Approach: To choose a sub-set sum of all of whose sub-sets is divisible by x, all the elements of the sub-set must be divisible by x. So,
- Count all the elements from the array that is divisible by x and store them in a variable count.
- Now, all possible sub-sets satisfying the condition will be 2count – 1
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#define ll long long int
using namespace std;
ll count( int arr[], int n, int x)
{
if (x == 1) {
ll ans = pow (2, n) - 1;
return ans;
}
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % x == 0)
count++;
}
ll ans = pow (2, count) - 1;
return ans;
}
int main()
{
int arr[] = { 2, 4, 3, 5 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 1;
cout << count(arr, n, x) << endl;
return 0;
}
|
Java
import java.util.*;
class solution
{
static long count( int arr[], int n, int x)
{
if (x == 1 ) {
long ans = ( long )Math.pow( 2 , n) - 1 ;
return ans;
}
int count = 0 ;
for ( int i = 0 ; i < n; i++) {
if (arr[i] % x == 0 )
count++;
}
long ans = ( long )Math.pow( 2 , count) - 1 ;
return ans;
}
public static void main(String args[])
{
int []arr = { 2 , 4 , 3 , 5 };
int n = arr.length;
int x = 1 ;
System.out.println(count(arr, n, x));
}
}
|
Python3
def count(arr, n, x) :
if (x = = 1 ) :
ans = pow ( 2 , n) - 1
return ans;
count = 0
for i in range (n) :
if (arr[i] % x = = 0 ) :
count + = 1
ans = pow ( 2 , count) - 1
return ans
if __name__ = = "__main__" :
arr = [ 2 , 4 , 3 , 5 ]
n = len (arr)
x = 1
print (count(arr, n, x))
|
C#
using System;
public class GFG{
static double count( int []arr, int n, int x)
{
double ans=0;
if (x == 1) {
ans = (Math.Pow(2, n) - 1);
return ans;
}
int count = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] % x == 0)
count++;
}
ans = (Math.Pow(2, count) - 1);
return ans;
}
static public void Main (){
int []arr = { 2, 4, 3, 5 };
int n = arr.Length;
int x = 1;
Console.WriteLine(count(arr, n, x));
}
}
|
PHP
<?php
function count_t( $arr , $n , $x )
{
if ( $x == 1) {
$ans = pow(2, $n ) - 1;
return $ans ;
}
$count = 0;
for ( $i = 0; $i < $n ; $i ++) {
if ( $arr [ $i ] % $x == 0)
$count ++;
}
$ans = pow(2, $count ) - 1;
return $ans ;
}
$arr = array ( 2, 4, 3, 5 );
$n = sizeof( $arr ) / sizeof( $arr [0]);
$x = 1;
echo count_t( $arr , $n , $x );
#This code is contributed by akt_mit
?>
|
Javascript
<script>
function count(arr, n, x)
{
let ans=0;
if (x == 1) {
ans = (Math.pow(2, n) - 1);
return ans;
}
let count = 0;
for (let i = 0; i < n; i++) {
if (arr[i] % x == 0)
count++;
}
ans = (Math.pow(2, count) - 1);
return ans;
}
let arr = [ 2, 4, 3, 5 ];
let n = arr.length;
let x = 1;
document.write(count(arr, n, x));
</script>
|
Complexity Analysis:
- Time complexity: O(n) because using a for loop
- Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...