Length of largest subarray whose all elements Powerful number | Java program to find the length of the largest sub - array of an array every element of whose is a powerful number ; Function to check if the number is powerful ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; If n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then 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. ; Function to return the length of the largest sub - array of an array every element of whose is a powerful number ; If arr [ i ] is a Powerful number ; Driver code
import java . util . * ; class solution { 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 contiguousPowerfulNumber ( int [ ] arr , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = arr . length ; System . out . println ( contiguousPowerfulNumber ( arr , n ) ) ; } }