给定一个由N个整数组成的数组arr [] ,任务是从一个数组中找到所有子集的和,其总和为一个完美数。
例子:
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
递归方法:想法是从给定数组生成所有可能的子集,并打印总和为理想数的那些子集的总和。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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) :
# Prthe 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
C++
// C++ program for the above approach
#include
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.
6
时间复杂度: O(M * 2 N ),其中M是数组arr []的元素之和
辅助空间: O(1)
迭代方法:由于大小为N的数组中有2 N个可能的子集,所以我们的想法是将循环从0迭代到2 N – 1,并针对每个数字,在的二进制表示中选择与1 s对应的所有数组元素。当前数字,然后检查所选元素的总和是否为理想数字。请按照以下步骤解决问题:
- 使用变量i在[0,2 N – 1]范围内进行迭代,并执行以下步骤:
- 初始化一个变量,用0表示S ,以存储当前子集的总和。
- 使用变量j遍历数组arr []并执行以下步骤:
- 如果第j个位置 在i的二进制表示形式中设置,将arr [j]的值添加到S。
- 检查S是否为整数,如果发现为真,则打印S的值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
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.
6
时间复杂度: O((N + M)* 2 N ),其中M是数组元素arr []的总和
辅助空间: O(1)