Sum of all subsets whose sum is a Perfect Number from a given array
Last Updated :
22 Nov, 2021
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check is a given number
// is a perfect number or not
int isPerfect(int x)
{
// Stores the sum of its divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for (int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find sum of all
// subsets from an array whose
// sum is a perfect number
void subsetSum(int arr[], int l,
int r, int sum = 0)
{
// Print the current subset sum
// if it is a perfect number
if (l > r) {
// Check if sum is a
// perfect number or not
if (isPerfect(sum)) {
cout << sum << " ";
}
return;
}
// Calculate sum of the subset
// including arr[l]
subsetSum(arr, l + 1, r, sum + arr[l]);
// Calculate sum of the subset
// excluding arr[l]
subsetSum(arr, l + 1, r, sum);
}
// Driver Code
int main()
{
int arr[] = { 5, 4, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
subsetSum(arr, 0, N - 1);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG{
// Function to check is a given number
// is a perfect number or not
static int isPerfect(int x)
{
// Stores the sum of its divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for(int i = 2; i <= x / 2; ++i)
{
if (x % i == 0)
{
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x)
{
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find sum of all
// subsets from an array whose
// sum is a perfect number
static void subsetSum(int[] arr, int l,
int r, int sum)
{
// Print the current subset sum
// if it is a perfect number
if (l > r)
{
// Check if sum is a
// perfect number or not
if (isPerfect(sum) != 0)
{
System.out.print(sum + " ");
}
return;
}
// Calculate sum of the subset
// including arr[l]
subsetSum(arr, l + 1, r, sum + arr[l]);
// Calculate sum of the subset
// excluding arr[l]
subsetSum(arr, l + 1, r, sum);
}
// Driver code
public static void main(String[] args)
{
int[] arr = { 5, 4, 6 };
int N = arr.length;
subsetSum(arr, 0, N - 1, 0);
}
}
// This code is contributed by code_hunt
Python3
# Python3 program for the above approach
import math
# Function to check is a given number
# is a perfect number or not
def isPerfect(x) :
# Stores the sum of its divisors
sum_div = 1
# Add all divisors of x to sum_div
for i in range(2, (x // 2) + 1) :
if (x % i == 0) :
sum_div += i
# If the sum of divisors is equal
# to the given number, return true
if (sum_div == x) :
return 1
# Otherwise, return false
else :
return 0
# Function to find sum of all
# subsets from an array whose
# sum is a perfect number
def subsetSum(arr, l,
r, sum) :
# Print current subset sum
# if it is a perfect number
if (l > r) :
# Check if sum is a
# perfect number or not
if (isPerfect(sum) != 0) :
print(sum, end = " ")
return
# Calculate sum of the subset
# including arr[l]
subsetSum(arr, l + 1, r, sum + arr[l])
# Calculate sum of the subset
# excluding arr[l]
subsetSum(arr, l + 1, r, sum)
# Driver Code
arr = [ 5, 4, 6 ]
N = len(arr)
subsetSum(arr, 0, N - 1, 0)
# This code is contributed by sanjoy_62.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check is a given number
// is a perfect number or not
static int isPerfect(int x)
{
// Stores the sum of its divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for(int i = 2; i <= x / 2; ++i)
{
if (x % i == 0)
{
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x)
{
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find sum of all
// subsets from an array whose
// sum is a perfect number
static void subsetSum(int[] arr, int l,
int r, int sum = 0)
{
// Print the current subset sum
// if it is a perfect number
if (l > r)
{
// Check if sum is a
// perfect number or not
if (isPerfect(sum) != 0)
{
Console.Write(sum + " ");
}
return;
}
// Calculate sum of the subset
// including arr[l]
subsetSum(arr, l + 1, r, sum + arr[l]);
// Calculate sum of the subset
// excluding arr[l]
subsetSum(arr, l + 1, r, sum);
}
// Driver code
static void Main()
{
int[] arr = { 5, 4, 6 };
int N = arr.Length;
subsetSum(arr, 0, N - 1);
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// javascript program for the above approach
// Function to check is a given number
// is a perfect number or not
function isPerfect(x) {
// Stores the sum of its divisors
var sum_div = 1;
// Add all divisors of x to sum_div
for (i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find sum of all
// subsets from an array whose
// sum is a perfect number
function subsetSum(arr , l , r , sum) {
// Print the current subset sum
// if it is a perfect number
if (l > r) {
// Check if sum is a
// perfect number or not
if (isPerfect(sum) != 0) {
document.write(sum + " ");
}
return;
}
// Calculate sum of the subset
// including arr[l]
subsetSum(arr, l + 1, r, sum + arr[l]);
// Calculate sum of the subset
// excluding arr[l]
subsetSum(arr, l + 1, r, sum);
}
// Driver code
var arr = [ 5, 4, 6 ];
var N = arr.length;
subsetSum(arr, 0, N - 1, 0);
// This code contributed by gauravrajput1
</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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check is a given
// number is a perfect number or not
int isPerfect(int x)
{
// Stores sum of divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for (int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find the sum of all the
// subsets from an array whose sum is
// a perfect number
void subsetSum(int arr[], int n)
{
// Stores the total number of
// subsets, i.e. 2 ^ n
long long total = 1 << n;
// Consider all numbers from 0 to 2 ^ n - 1
for (long long i = 0; i < total; i++) {
long long sum = 0;
// Consider array elements from
// positions of set bits in the
// binary representation of n
for (int j = 0; j < n; j++)
if (i & (1 << j))
sum += arr[j];
// If sum of chosen elements
// is a perfect number
if (isPerfect(sum)) {
cout << sum << " ";
}
}
}
// Driver Code
int main()
{
int arr[] = { 5, 4, 6 };
int N = sizeof(arr) / sizeof(arr[0]);
subsetSum(arr, N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to check is a given
// number is a perfect number or not
static int isPerfect(int x)
{
// Stores sum of divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for (int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find the sum of all the
// subsets from an array whose sum is
// a perfect number
static void subsetSum(int arr[], int n)
{
// Stores the total number of
// subsets, i.e. 2 ^ n
long total = 1 << n;
// Consider all numbers from 0 to 2 ^ n - 1
for (long i = 0; i < total; i++) {
int sum = 0;
// Consider array elements from
// positions of set bits in the
// binary representation of n
for (int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// If sum of chosen elements
// is a perfect number
if (isPerfect(sum) != 0) {
System.out.print(sum + " ");
}
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 5, 4, 6 };
int N = arr.length;
subsetSum(arr, N);
}
}
// This code is contributed by souravghosh0416.
Python3
# Python3 program for the above approach
# Function to check is a given
# number is a perfect number or not
def isPerfect(x):
# Stores sum of divisors
sum_div = 1
# Add all divisors of x to sum_div
for i in range(2, int(x/2 + 1)):
if (x % i == 0):
sum_div = sum_div + i
# If the sum of divisors is equal
# to the given number, return true
if (sum_div == x):
return 1
# Otherwise, return false
else:
return 0
# Function to find the sum of all the
# subsets from an array whose sum is
# a perfect number
def subsetSum(arr, n):
# Stores the total number of
# subsets, i.e. 2 ^ n
total = 1 << n
# Consider all numbers from 0 to 2 ^ n - 1
for i in range(total):
sum = 0
# Consider array elements from
# positions of set bits in the
# binary representation of n
for j in range(n):
if (i & (1 << j) != 0):
sum = sum + arr[j]
# If sum of chosen elements
# is a perfect number
if (isPerfect(sum)):
print(sum, " ")
# Driver Code
arr = [5, 4, 6]
N = len(arr)
subsetSum(arr, N)
# This code is contributed by Dharanendra L V.
C#
// C# program for the above approach
using System;
class GFG{
// Function to check is a given
// number is a perfect number or not
static int isPerfect(int x)
{
// Stores sum of divisors
int sum_div = 1;
// Add all divisors of x to sum_div
for (int i = 2; i <= x / 2; ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find the sum of all the
// subsets from an array whose sum is
// a perfect number
static void subsetSum(int[] arr, int n)
{
// Stores the total number of
// subsets, i.e. 2 ^ n
long total = 1 << n;
// Consider all numbers from 0 to 2 ^ n - 1
for (long i = 0; i < total; i++) {
int sum = 0;
// Consider array elements from
// positions of set bits in the
// binary representation of n
for (int j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// If sum of chosen elements
// is a perfect number
if (isPerfect(sum) != 0) {
Console.Write(sum + " ");
}
}
}
// Driver Code
static public void Main()
{
int[] arr = { 5, 4, 6 };
int N = arr.Length;
subsetSum(arr, N);
}
}
// This code is contributed by splevel62.
JavaScript
<script>
// javascript program for the above approach
// Function to check is a given
// number is a perfect number or not
function isPerfect(x)
{
// Stores sum of divisors
var sum_div = 1;
// Add all divisors of x to sum_div
for (var i = 2; i <= parseInt(x / 2); ++i) {
if (x % i == 0) {
sum_div += i;
}
}
// If the sum of divisors is equal
// to the given number, return true
if (sum_div == x) {
return 1;
}
// Otherwise, return false
else
return 0;
}
// Function to find the sum of all the
// subsets from an array whose sum is
// a perfect number
function subsetSum(arr , n)
{
// Stores the total number of
// subsets, i.e. 2 ^ n
var total = 1 << n;
// Consider all numbers from 0 to 2 ^ n - 1
for (i = 0; i < total; i++) {
var sum = 0;
// Consider array elements from
// positions of set bits in the
// binary representation of n
for (j = 0; j < n; j++)
if ((i & (1 << j)) != 0)
sum += arr[j];
// If sum of chosen elements
// is a perfect number
if (isPerfect(sum) != 0) {
document.write(sum + " ");
}
}
}
// Driver Code
var arr = [ 5, 4, 6 ];
var N = arr.length;
subsetSum(arr, N);
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O((N + M) * 2N), where M is the sum of the elements of the array, arr[]
Auxiliary Space: O(1)
Similar Reads
Maximum subsequence sum from a given array which is a perfect square Given an array arr[], the task is to find the sum of a subsequence that forms a perfect square. If there are multiple subsequences having a sum equal to a perfect square, print the maximum sum.Explanation: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9} Output: 36 Explanation: Maximum possible sum which
14 min read
Print all Perfect Numbers from an array whose sum of digits is also a Perfect Number Given an array arr[] of size N, the task is to print all the perfect numbers from an array whose sum of digits is also a perfect number. Examples: Input: arr[] = { 3, 8, 12, 28, 6 }Output: 6Explanation: The array element arr[4] (= 6) is a perfect number. The array element arr[3] (= 28) is a perfect
8 min read
Count of pairs in an array whose sum is a perfect square Given an array arr of distinct elements, the task is to find the total number of two element pairs from the array whose sum is a perfect square. Examples: Input: arr[] = {2, 3, 6, 9, 10, 20} Output: 2 Only possible pairs are (3, 6) and (6, 10) Input: arr[] = {9, 2, 5, 1} Output: 0 Naive Approach: Us
12 min read
Sum of all perfect numbers present in an array Given an array arr[] containing N positive integer. The task is to find the sum of all the perfect numbers from the array. A number is perfect if it is equal to the sum of its proper divisors i.e. the sum of its positive divisors excluding the number itself. Examples: Input: arr[] = {3, 6, 9} Output
5 min read
Sum of sum of all subsets of a set formed by first N natural numbers Given N, and ff(N) = f(1) + f(2) + ...... + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007. Examples: Input: 2 Output: 7 f(1) + f(2) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 Input: 3 Output: 31 f(1) + f(2) + f(3) f(1)
11 min read