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> |
6
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:
- If the jth position is set in the binary representation of i, add the value of arr[j] to S.
- 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> |
6
Time Complexity: O((N + M) * 2N), where M is the sum of the elements of the array, arr[]
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!