Skip to content
Related Articles

Related Articles

Length of Longest Perfect number Subsequence in an Array

View Discussion
Improve Article
Save Article
  • Last Updated : 03 Jun, 2021

Given an array arr[] containing non-negative integers of length N, the task is to print the length of the longest subsequence of the Perfect number in the array. 

A number is a perfect number if it is equal to the sum of its proper divisors, that is, the sum of its positive divisors excluding the number itself.  

Examples: 

Input: arr[] = { 3, 6, 11, 2, 28, 21, 8128 } 
Output:
Explanation: 
The longest perfect number subsequence is {6, 28, 8128} and hence the answer is 3.

Input:arr[] = { 6, 4, 10, 13, 9, 25 } 
Output:
Explanation: 
The longest perfect number subsequence is {6} and hence the answer is 1. 

Approach:
To solve the problem mentioned above, follow the steps given below: 

  • Traverse the given array and for each element in the array, check if it is a perfect number or not.
  • If the element is a perfect number, it will be in the Longest Perfect number Subsequence. Hence, increment the required length of the Longest Perfect number Subsequence by 1

Below is the implementation of the above approach: 

C++




// C++ program to find the length of
// Longest Perfect number Subsequence in an Array
 
#include <bits/stdc++.h>
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

Javascript




<script>
 
// Javascript program to find the length of
// Longest Perfect number Subsequence in an Array
 
// Function to check if
// the number is a Perfect number
function isPerfect(n)
{
    // To store sum of divisors
    var sum = 1;
 
    // Find all divisors and add them
    for (var 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
function longestPerfectSubsequence(arr, n)
{
    var answer = 0;
 
    // Find the length of longest
    // Perfect number subsequence
    for (var i = 0; i < n; i++) {
        if (isPerfect(arr[i]))
            answer++;
    }
 
    return answer;
}
 
// Driver code
var arr = [3, 6, 11, 2, 28, 21, 8128];
var n = arr.length;
document.write( longestPerfectSubsequence(arr, n));
 
 
</script>

Output: 

3

 

Time Complexity: O(N×√N)
Auxiliary Space Complexity: O(1)
 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!