Length of longest Powerful number subsequence in an Array
Last Updated :
16 Apr, 2021
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: 2
Explanation:
Longest Powerful number Subsequence is {4, 9} and hence the answer is 2.
Input: arr[] = { 6, 4, 10, 13, 9, 25 }
Output: 3
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++
#include <bits/stdc++.h>
using namespace std;
bool isPowerful( int n)
{
while (n % 2 == 0) {
int power = 0;
while (n % 2 == 0) {
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for ( int factor = 3;
factor <= sqrt (n);
factor += 2) {
int power = 0;
while (n % factor == 0) {
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
int longestPowerfulSubsequence(
int arr[], int n)
{
int answer = 0;
for ( int i = 0; i < n; i++) {
if (isPowerful(arr[i]))
answer++;
}
return answer;
}
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
class GFG{
static boolean isPowerful( int n)
{
while (n % 2 == 0 )
{
int power = 0 ;
while (n % 2 == 0 )
{
n /= 2 ;
power++;
}
if (power == 1 )
return false ;
}
for ( int factor = 3 ;
factor <= Math.sqrt(n);
factor += 2 )
{
int power = 0 ;
while (n % factor == 0 )
{
n = n / factor;
power++;
}
if (power == 1 )
return false ;
}
return (n == 1 );
}
static int longestPowerfulSubsequence( int arr[],
int n)
{
int answer = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (isPowerful(arr[i]))
answer++;
}
return answer;
}
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" );
}
}
|
Python3
import math
def isPowerful(n):
while (n % 2 = = 0 ):
power = 0
while (n % 2 = = 0 ):
n / / = 2
power + = 1
if (power = = 1 ):
return False
for factor in range ( 3 , int (math.sqrt(n)) + 1 , 2 ):
power = 0
while (n % factor = = 0 ):
n = n / / factor
power + = 1
if (power = = 1 ):
return False
return (n = = 1 )
def longestPowerfulSubsequence(arr, n):
answer = 0
for i in range (n):
if (isPowerful(arr[i])):
answer + = 1
return answer
arr = [ 6 , 4 , 10 , 13 , 9 , 25 ]
n = len (arr)
print (longestPowerfulSubsequence(arr, n))
|
C#
using System;
class GFG{
static bool isPowerful( int n)
{
while (n % 2 == 0)
{
int power = 0;
while (n % 2 == 0)
{
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for ( int factor = 3;
factor <= Math.Sqrt(n);
factor += 2)
{
int power = 0;
while (n % factor == 0)
{
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
static int longestPowerfulSubsequence( int []arr,
int n)
{
int answer = 0;
for ( int i = 0; i < n; i++)
{
if (isPowerful(arr[i]))
answer++;
}
return answer;
}
public static void Main(String[] args)
{
int []arr = { 6, 4, 10, 13, 9, 25 };
int n = arr.Length;
Console.Write(longestPowerfulSubsequence(arr,
n) + "\n" );
}
}
|
Javascript
<script>
function isPowerful(n)
{
while (n % 2 == 0)
{
let power = 0;
while (n % 2 == 0)
{
n /= 2;
power++;
}
if (power == 1)
return false ;
}
for (let factor = 3;
factor <= Math.sqrt(n);
factor += 2)
{
let power = 0;
while (n % factor == 0)
{
n = n / factor;
power++;
}
if (power == 1)
return false ;
}
return (n == 1);
}
function longestPowerfulSubsequence(arr,
n)
{
let answer = 0;
for (let i = 0; i < n; i++)
{
if (isPowerful(arr[i]))
answer++;
}
return answer;
}
let arr = [ 6, 4, 10, 13, 9, 25 ];
let n = arr.length;
document.write(longestPowerfulSubsequence(arr,
n) + "\n" );
</script>
|
Time Complexity: O(N*?N)
Auxiliary Space Complexity: O(1)
Share your thoughts in the comments
Please Login to comment...