Open In App

Length of longest Powerful number subsequence in an Array

Last Updated : 16 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

A number n is said to be Powerful Number if, for every prime factor p of it, p2 also divides it.

Examples: 
 

Input: arr[] = { 3, 4, 11, 2, 9, 21 } 
Output:
Explanation: 
Longest Powerful number Subsequence is {4, 9} and hence the answer is 2.
Input: arr[] = { 6, 4, 10, 13, 9, 25 } 
Output:
Explanation: 
Longest Powerful number Subsequence is {4, 9, 25} and hence the answer is 3.

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

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

Below is the implementation of the above approach:
 

C++




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




<script>
     
// Javascript program to find the length of
// Longest Powerful Subsequence in an Array
 
// Function to check if the number is powerful
function isPowerful(n)
{
   
    // First divide the number repeatedly by 2
    while (n % 2 == 0)
    {
        let 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(let factor = 3;
            factor <= Math.sqrt(n);
            factor += 2)
    {
   
       // Find highest power of "factor"
       // that divides n
       let 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
function longestPowerfulSubsequence(arr,
                                      n)
{
    let answer = 0;
   
    for(let i = 0; i < n; i++)
    {
       if (isPowerful(arr[i]))
       answer++;
    }
       
    return answer;
}
     
// Driver code
    let arr = [ 6, 4, 10, 13, 9, 25 ];
    let n = arr.length;
    document.write(longestPowerfulSubsequence(arr,
                                                n) + "\n");
 
// This code is contributed by souravghosh0416.
</script>


Output: 

3

 

Time Complexity: O(N*?N)
Auxiliary Space Complexity: O(1)
 



Similar Reads

Longest Increasing Subsequence using Longest Common Subsequence Algorithm
Given an array arr[] of N integers, the task is to find and print the Longest Increasing Subsequence.Examples: Input: arr[] = {12, 34, 1, 5, 40, 80} Output: 4 {12, 34, 40, 80} and {1, 5, 40, 80} are the longest increasing subsequences.Input: arr[] = {10, 22, 9, 33, 21, 50, 41, 60, 80} Output: 6 Prerequisite: LCS, LISApproach: The longest increasing
12 min read
Length of largest subarray whose all elements Powerful number
Given an array arr[] of integer elements, the task is to find the length of the largest sub-array of arr[] such that all the elements of the sub-array are Powerful number. A number n is said to be Powerful Number if, for every prime factor p of it, p2 also divides it. Examples: Input: arr[] = {1, 7, 36, 4, 6, 28, 4} Output:2 Maximum length sub-arra
9 min read
Length of longest Palindromic Subsequence of even length with no two adjacent characters same
Given a string str, the task is to find the length of the longest palindromic subsequence of even length with no two adjacent characters same except the middle characters. Examples: Input: str = "abscrcdba" Output: 6 Explanation: abccba is the required string which has no two consecutive characters same except the middle characters. Hence the lengt
11 min read
Length of Longest Perfect number Subsequence in an Array
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
7 min read
Longest Subsequence with absolute difference of pairs as at least Subsequence's maximum
Given an array arr[] of length N. The task is to find the length of the longest subsequence of the array such that the absolute difference between any pair of elements is greater than or equal to the maximum element in that subsequence. Examples: Input: N = 6, arr[] = {1, 1, 0, 0, 0, 0}Output: 4Explanation: Considering 0 as max element of subsequen
7 min read
Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
Given a sorted array of N integers. The task is to find the longest subsequence such that every element in the subsequence is reachable by multiplying any prime number to the previous element in the subsequence.Note: A[i] &lt;= 105 Examples: Input: a[] = {3, 5, 6, 12, 15, 36} Output 4 The longest subsequence is {3, 6, 12, 36} 6 = 3*2 12 = 6*2 36 =
20 min read
Length of longest subsequence having sum of digits of each element as a Composite Number
Given an array arr[] consisting of non-negative integers, the task is to print the length of the longest subsequence from the given array whose sum of digits of each element is a composite numbers. Examples: Input: arr[] = {13, 55, 7, 3, 5, 21, 233, 144, 89}Output: 4Explanation: Following array elements have sum of digits equal to a composite numbe
9 min read
Length of Longest Prime Subsequence in an Array
Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of prime numbers in the array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Longest Prime Subsequence is {3, 2, 11} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9, 25 } Output: 1 Approach: Traverse the given array.
6 min read
Length of longest subsequence of Fibonacci Numbers in an Array
Given an array arr containing non-negative integers, the task is to print the length of the longest subsequence of Fibonacci numbers in this array.Examples: Input: arr[] = { 3, 4, 11, 2, 9, 21 } Output: 3 Here, the subsequence is {3, 2, 21} and hence the answer is 3.Input: arr[] = { 6, 4, 10, 13, 9, 25 } Output: 1 Here, the subsequence is {1} and h
5 min read
Maximize length of longest increasing prime subsequence from the given array
Given an array, arr[] of size N, the task is to find the length of the longest increasing prime subsequence possible by performing the following operations. If arr[i] is already a prime number, no need to update arr[i].Update non-prime arr[i] to the closest prime number less than arr[i].Update non-prime arr[i] to the closest prime number greater th
13 min read