给定数组arr []包含长度为N的非负整数,任务是打印数组中Perfect数字s的最长子序列的长度。
A number is a perfect number if it is equal to the sum of its proper divisors, that is, sum of its positive divisors excluding the number itself.
例子:
Input: arr[] = { 3, 6, 11, 2, 28, 21, 8128 }
Output: 3
Explanation:
Longest Perfect number subsequence is {6, 28, 8128} and hence the answer is 3.
Input:arr[] = { 6, 4, 10, 13, 9, 25 }
Output: 1
Explanation:
Longest Perfect number subsequence is {6} and hence the answer is 1.
方法:
要解决上述问题,请执行以下步骤:
- 遍历给定的数组,并检查该数组中的每个元素是否为完美数。
- 如果元素是一个完美数,它将在最长完美数子序列中。因此,将最长完美数子序列的所需长度增加1
下面是上述方法的实现:
C++
// C++ program to find the length of
// Longest Perfect number Subsequence in an Array
#include
using namespace std;
// Function to check if
// the number is a Perfect number
bool isPerfect(long long int n)
{
// To store sum of divisors
long long int sum = 1;
// Find all divisors and add them
for (long long int i = 2; i * i <= n; i++) {
if (n % i == 0) {
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
// Check if sum of divisors is equal to
// n, then n is a perfect number
if (sum == n && n != 1)
return true;
return false;
}
// Function to find the longest subsequence
// which contain all Perfect numbers
int longestPerfectSubsequence(int arr[], int n)
{
int answer = 0;
// Find the length of longest
// Perfect number subsequence
for (int i = 0; i < n; i++) {
if (isPerfect(arr[i]))
answer++;
}
return answer;
}
// Driver code
int main()
{
int arr[] = { 3, 6, 11, 2, 28, 21, 8128 };
int n = sizeof(arr) / sizeof(arr[0]);
cout << longestPerfectSubsequence(arr, n) << endl;
return 0;
}
Java
// Java program to find the length of
// longest perfect number subsequence
// in an array
class GFG {
// Function to check if the
// number is a perfect number
static boolean isPerfect(long n)
{
// To store sum of divisors
long sum = 1;
// Find all divisors and add them
for(long i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
// Check if sum of divisors is equal
// to n, then n is a perfect number
if (sum == n && n != 1)
{
return true;
}
return false;
}
// Function to find the longest subsequence
// which contain all Perfect numbers
static int longestPerfectSubsequence(int arr[],
int n)
{
int answer = 0;
// Find the length of longest
// perfect number subsequence
for(int i = 0; i < n; i++)
{
if (isPerfect(arr[i]) == true)
answer++;
}
return answer;
}
// Driver code
public static void main (String[] args)
{
int arr[] = { 3, 6, 11, 2, 28, 21, 8128 };
int n = arr.length;
System.out.println(longestPerfectSubsequence(arr, n));
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find the length of
# Longest Perfect number Subsequence in an Array
# Function to check if
# the number is Perfect number
def isPerfect( n ):
# To store sum of divisors
sum = 1
# Find all divisors and add them
i = 2
while i * i <= n:
if n % i == 0:
sum = sum + i + n / i
i += 1
# Check if sum of divisors is equal to
# n, then n is a perfect number
return (True if sum == n and n != 1 else False)
# Function to find the longest subsequence
# which contain all Perfect numbers
def longestPerfectSubsequence( arr, n):
answer = 0
# Find the length of longest
# Perfect number subsequence
for i in range (n):
if (isPerfect(arr[i])):
answer += 1
return answer
# Driver code
if __name__ == "__main__":
arr = [ 3, 6, 11, 2, 28, 21, 8128 ]
n = len(arr)
print (longestPerfectSubsequence(arr, n))
C#
// C# program to find the length of
// longest perfect number subsequence
// in an array
using System;
class GFG {
// Function to check if the
// number is a perfect number
static bool isPerfect(long n)
{
// To store sum of divisors
long sum = 1;
// Find all divisors and add them
for(long i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
if (i * i != n)
sum = sum + i + n / i;
else
sum = sum + i;
}
}
// Check if sum of divisors is equal
// to n, then n is a perfect number
if (sum == n && n != 1)
{
return true;
}
return false;
}
// Function to find the longest subsequence
// which contain all perfect numbers
static int longestPerfectSubsequence(int []arr,
int n)
{
int answer = 0;
// Find the length of longest
// perfect number subsequence
for(int i = 0; i < n; i++)
{
if (isPerfect(arr[i]) == true)
answer++;
}
return answer;
}
// Driver code
public static void Main (string[] args)
{
int []arr = { 3, 6, 11, 2, 28, 21, 8128 };
int n = arr.Length;
Console.WriteLine(longestPerfectSubsequence(arr, n));
}
}
// This code is contributed by AnkitRai01
输出:
3
时间复杂度: O(N×√N)
辅助空间复杂度: O(1)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。