📜  数组中最长强力数子序列的长度

📅  最后修改于: 2021-09-06 06:49:45             🧑  作者: Mango

给定一个包含长度为N 的非负整数的数组 arr[] ,任务是打印数组中强大数的最长子序列的长度。

例子:

方法:要解决上述问题,我们必须按照以下步骤操作:

  • 遍历给定的数组,对于数组中的每个元素,检查它是否是强大的数字。
  • 如果元素是一个强大的数字,它将在最长的强大数字子序列中。
  • 因此,将最长最强数子序列的所需长度增加 1
  • 达到数组大小后返回最终长度。

下面是上述方法的实现:

C++
// C++ program to find the length of
// Longest Powerful Subsequence in an Array
 
#include 
using namespace std;
 
// Function to check if the number is powerful
bool isPowerful(int n)
{
    // First divide the number repeatedly by 2
    while (n % 2 == 0) {
        int power = 0;
        while (n % 2 == 0) {
            n /= 2;
            power++;
        }
 
        // Check if only 2^1 divides n,
        // then return false
        if (power == 1)
            return false;
    }
 
    // Check if n is not a power of 2
    // then this loop will execute
    // repeat above process
    for (int factor = 3;
         factor <= sqrt(n);
         factor += 2) {
 
        // Find highest power of "factor"
        // that divides n
        int power = 0;
        while (n % factor == 0) {
            n = n / factor;
            power++;
        }
 
        // If only factor^1 divides n,
        // then return false
        if (power == 1)
            return false;
    }
 
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers
    // are not powerful, we return
    // false if n is not 1.
    return (n == 1);
}
 
// Function to find the longest subsequence
// which contain all powerful numbers
int longestPowerfulSubsequence(
    int arr[], int n)
{
    int answer = 0;
 
    for (int i = 0; i < n; i++) {
        if (isPowerful(arr[i]))
            answer++;
    }
 
    return answer;
}
 
// Driver code
int main()
{
    int arr[] = { 6, 4, 10, 13, 9, 25 };
 
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << longestPowerfulSubsequence(arr, n)
         << endl;
 
    return 0;
}


Java
// Java program to find the length of
// Longest Powerful Subsequence in an Array
class GFG{
 
// Function to check if the number is powerful
static boolean isPowerful(int n)
{
 
    // First divide the number repeatedly by 2
    while (n % 2 == 0)
    {
        int power = 0;
        while (n % 2 == 0)
        {
            n /= 2;
            power++;
        }
 
        // Check if only 2^1 divides n,
        // then return false
        if (power == 1)
            return false;
    }
 
    // Check if n is not a power of 2
    // then this loop will execute
    // repeat above process
    for(int factor = 3;
            factor <= Math.sqrt(n);
            factor += 2)
    {
 
       // Find highest power of "factor"
       // that divides n
       int power = 0;
       while (n % factor == 0)
       {
           n = n / factor;
           power++;
       }
        
       // If only factor^1 divides n,
       // then return false
       if (power == 1)
       return false;
         
    }
 
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers
    // are not powerful, we return
    // false if n is not 1.
    return (n == 1);
}
 
// Function to find the longest subsequence
// which contain all powerful numbers
static int longestPowerfulSubsequence(int arr[],
                                      int n)
{
    int answer = 0;
 
    for(int i = 0; i < n; i++)
    {
       if (isPowerful(arr[i]))
       answer++;
    }
     
    return answer;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 6, 4, 10, 13, 9, 25 };
    int n = arr.length;
 
    System.out.print(longestPowerfulSubsequence(arr,
                                                n) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to find the length of
# Longest Powerful Subsequence in an Array
import math
 
# Function to check if the number is powerful
def isPowerful(n):
 
    # First divide the number repeatedly by 2
    while (n % 2 == 0):
        power = 0
         
        while (n % 2 == 0):
            n //= 2
            power += 1
         
        # Check if only 2^1 divides n,
        # then return false
        if (power == 1):
            return False
     
    # Check if n is not a power of 2
    # then this loop will execute
    # repeat above process
    for factor in range(3, int(math.sqrt(n)) + 1, 2):
 
        # Find highest power of "factor"
        # that divides n
        power = 0
        while (n % factor == 0):
            n = n // factor
            power += 1
         
        # If only factor^1 divides n,
        # then return false
        if (power == 1):
            return False
     
    # n must be 1 now
    # if it is not a prime number.
    # Since prime numbers
    # are not powerful, we return
    # false if n is not 1.
    return (n == 1)
 
# Function to find the longest subsequence
# which contain all powerful numbers
def longestPowerfulSubsequence(arr, n):
     
    answer = 0
 
    for i in range(n):
        if (isPowerful(arr[i])):
            answer += 1
     
    return answer
 
# Driver code
arr = [ 6, 4, 10, 13, 9, 25 ]
 
n = len(arr)
 
print(longestPowerfulSubsequence(arr, n))
 
# This code is contributed by sanjoy_62


C#
// C# program to find the length of
// longest Powerful Subsequence in
// an array
using System;
class GFG{
 
// Function to check if the
// number is powerful
static bool isPowerful(int n)
{
 
    // First divide the number
    // repeatedly by 2
    while (n % 2 == 0)
    {
        int power = 0;
        while (n % 2 == 0)
        {
            n /= 2;
            power++;
        }
 
        // Check if only 2^1 divides
        // n, then return false
        if (power == 1)
            return false;
    }
 
    // Check if n is not a power of 2
    // then this loop will execute
    // repeat above process
    for(int factor = 3;
            factor <= Math.Sqrt(n);
            factor += 2)
    {
        
       // Find highest power of "factor"
       // that divides n
       int power = 0;
       while (n % factor == 0)
       {
           n = n / factor;
           power++;
       }
        
       // If only factor^1 divides n,
       // then return false
       if (power == 1)
           return false;
    }
     
    // n must be 1 now
    // if it is not a prime number.
    // Since prime numbers
    // are not powerful, we return
    // false if n is not 1.
    return (n == 1);
}
 
// Function to find the longest subsequence
// which contain all powerful numbers
static int longestPowerfulSubsequence(int []arr,
                                      int n)
{
    int answer = 0;
 
    for(int i = 0; i < n; i++)
    {
       if (isPowerful(arr[i]))
           answer++;
    }
    return answer;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 6, 4, 10, 13, 9, 25 };
    int n = arr.Length;
 
    Console.Write(longestPowerfulSubsequence(arr,
                                             n) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Javascript


输出:
3

时间复杂度: O(N*√N)
辅助空间复杂度: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live