Sum of all subsets whose sum is a Perfect Number from a given array
Given an array arr[] consisting of N integers, the task is to find the sum of all subsets from an array, whose sum is a Perfect Number.
Examples:
Input: arr[] = {5, 4, 6}
Output: 6
Explanation:
All possible subsets from the array arr[] are:
{5} ? Sum = 5
{4} ? Sum = 4.
{6} ? Sum = 6.
{5, 4} ? Sum = 9.
{5, 6} ? Sum = 11.
{4, 6} ? Sum = 10.
{5, 4, 6} ? Sum = 15.
Out of all subset sums, only 6 sums are found to be2` a perfect number.
Input: arr[] = {28, 6, 23, 3, 3}
Output: 28 6 6
Recursive Approach: The idea is to generate all possible subsets from the given array and print the sum of those subsets whose sum is a perfect number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPerfect( int x)
{
int sum_div = 1;
for ( int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
if (sum_div == x) {
return 1;
}
else
return 0;
}
void subsetSum( int arr[], int l,
int r, int sum = 0)
{
if (l > r) {
if (isPerfect(sum)) {
cout << sum << " " ;
}
return ;
}
subsetSum(arr, l + 1, r, sum + arr[l]);
subsetSum(arr, l + 1, r, sum);
}
int main()
{
int arr[] = { 5, 4, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
subsetSum(arr, 0, N - 1);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static int isPerfect( int x)
{
int sum_div = 1 ;
for ( int i = 2 ; i <= x / 2 ; ++i)
{
if (x % i == 0 )
{
sum_div += i;
}
}
if (sum_div == x)
{
return 1 ;
}
else
return 0 ;
}
static void subsetSum( int [] arr, int l,
int r, int sum)
{
if (l > r)
{
if (isPerfect(sum) != 0 )
{
System.out.print(sum + " " );
}
return ;
}
subsetSum(arr, l + 1 , r, sum + arr[l]);
subsetSum(arr, l + 1 , r, sum);
}
public static void main(String[] args)
{
int [] arr = { 5 , 4 , 6 };
int N = arr.length;
subsetSum(arr, 0 , N - 1 , 0 );
}
}
|
Python3
import math
def isPerfect(x) :
sum_div = 1
for i in range ( 2 , (x / / 2 ) + 1 ) :
if (x % i = = 0 ) :
sum_div + = i
if (sum_div = = x) :
return 1
else :
return 0
def subsetSum(arr, l,
r, sum ) :
if (l > r) :
if (isPerfect( sum ) ! = 0 ) :
print ( sum , end = " " )
return
subsetSum(arr, l + 1 , r, sum + arr[l])
subsetSum(arr, l + 1 , r, sum )
arr = [ 5 , 4 , 6 ]
N = len (arr)
subsetSum(arr, 0 , N - 1 , 0 )
|
C#
using System;
class GFG{
static int isPerfect( int x)
{
int sum_div = 1;
for ( int i = 2; i <= x / 2; ++i)
{
if (x % i == 0)
{
sum_div += i;
}
}
if (sum_div == x)
{
return 1;
}
else
return 0;
}
static void subsetSum( int [] arr, int l,
int r, int sum = 0)
{
if (l > r)
{
if (isPerfect(sum) != 0)
{
Console.Write(sum + " " );
}
return ;
}
subsetSum(arr, l + 1, r, sum + arr[l]);
subsetSum(arr, l + 1, r, sum);
}
static void Main()
{
int [] arr = { 5, 4, 6 };
int N = arr.Length;
subsetSum(arr, 0, N - 1);
}
}
|
Javascript
<script>
function isPerfect(x) {
var sum_div = 1;
for (i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
if (sum_div == x) {
return 1;
}
else
return 0;
}
function subsetSum(arr , l , r , sum) {
if (l > r) {
if (isPerfect(sum) != 0) {
document.write(sum + " " );
}
return ;
}
subsetSum(arr, l + 1, r, sum + arr[l]);
subsetSum(arr, l + 1, r, sum);
}
var arr = [ 5, 4, 6 ];
var N = arr.length;
subsetSum(arr, 0, N - 1, 0);
</script>
|
Time Complexity: O(M * 2N), where M is the sum of the elements of the array arr[]
Auxiliary Space: O(1)
Iterative Approach: Since there are 2N possible subsets from an array of size N, the idea is to iterate a loop from 0 to 2N – 1 and for every number, pick all array elements which correspond to 1s in the binary representation of the current number and then check if the sum of the chosen elements is a perfect number or not. Follow the steps below to solve the problem:
- Iterate in the range[0, 2N – 1] using the variable i and perform the following steps:
- Initialize a variable, say S with 0 to store the sum of the current subset.
- Traverse the array arr[] using the variable j and perform the following steps:
- Check if S is a perfect number or not, if found to be true print the value of S as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int isPerfect( int x)
{
int sum_div = 1;
for ( int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
if (sum_div == x) {
return 1;
}
else
return 0;
}
void subsetSum( int arr[], int n)
{
long long total = 1 << n;
for ( long long i = 0; i < total; i++) {
long long sum = 0;
for ( int j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
if (isPerfect(sum)) {
cout << sum << " " ;
}
}
}
int main()
{
int arr[] = { 5, 4, 6 };
int N = sizeof (arr) / sizeof (arr[0]);
subsetSum(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int isPerfect( int x)
{
int sum_div = 1 ;
for ( int i = 2 ; i <= x / 2 ; ++i) {
if (x % i == 0 ) {
sum_div += i;
}
}
if (sum_div == x) {
return 1 ;
}
else
return 0 ;
}
static void subsetSum( int arr[], int n)
{
long total = 1 << n;
for ( long i = 0 ; i < total; i++) {
int sum = 0 ;
for ( int j = 0 ; j < n; j++)
if ((i & ( 1 << j)) != 0 )
sum += arr[j];
if (isPerfect(sum) != 0 ) {
System.out.print(sum + " " );
}
}
}
public static void main(String[] args)
{
int arr[] = { 5 , 4 , 6 };
int N = arr.length;
subsetSum(arr, N);
}
}
|
Python3
def isPerfect(x):
sum_div = 1
for i in range ( 2 , int (x / 2 + 1 )):
if (x % i = = 0 ):
sum_div = sum_div + i
if (sum_div = = x):
return 1
else :
return 0
def subsetSum(arr, n):
total = 1 << n
for i in range (total):
sum = 0
for j in range (n):
if (i & ( 1 << j) ! = 0 ):
sum = sum + arr[j]
if (isPerfect( sum )):
print ( sum , " " )
arr = [ 5 , 4 , 6 ]
N = len (arr)
subsetSum(arr, N)
|
C#
using System;
class GFG{
static int isPerfect( int x)
{
int sum_div = 1;
for ( int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
if (sum_div == x) {
return 1;
}
else
return 0;
}
static void subsetSum( int [] arr, int n)
{
long total = 1 << n;
for ( long i = 0; i < total; i++) {
int sum = 0;
for ( int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
if (isPerfect(sum) != 0) {
Console.Write(sum + " " );
}
}
}
static public void Main()
{
int [] arr = { 5, 4, 6 };
int N = arr.Length;
subsetSum(arr, N);
}
}
|
Javascript
<script>
function isPerfect(x)
{
var sum_div = 1;
for ( var i = 2; i <= parseInt(x / 2); ++i) {
if (x % i == 0) {
sum_div += i;
}
}
if (sum_div == x) {
return 1;
}
else
return 0;
}
function subsetSum(arr , n)
{
var total = 1 << n;
for (i = 0; i < total; i++) {
var sum = 0;
for (j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
if (isPerfect(sum) != 0) {
document.write(sum + " " );
}
}
}
var arr = [ 5, 4, 6 ];
var N = arr.length;
subsetSum(arr, N);
</script>
|
Time Complexity: O((N + M) * 2N), where M is the sum of the elements of the array, arr[]
Auxiliary Space: O(1)
Last Updated :
22 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...