Generate a sequence with product N such that for every pair of indices ( i , j ) and i < j , arr [ j ] is divisible by arr [ i ] | Java program for the above approach ; Function to calculate the prime factors of N with their count ; Initialize a vector , say v ; Initialize the count ; Count the number of divisors ; Divide the value of N by 2 ; For factor 2 divides it ; Find all prime factors ; Count their frequency ; Push it to the vector ; Push N if it is not 1 ; Function to print the array that have the maximum size ; Stores the all prime factor and their powers ; Traverse the vector and find the maximum power of prime factor ; If max size is less than 2 ; Otherwise ; Print the maximum size of sequence ; Print the final sequence ; Print the prime factor ; Print the last value of the sequence ; Driver Code
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > primeFactor ( int N ) { Vector < pair > v = new Vector < > ( ) ; int count = 0 ; while ( ( N % 2 ) == 0 ) { N >>= 1 ; count ++ ; } if ( count != 0 ) v . add ( new pair ( 2 , count ) ) ; for ( int i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } if ( count != 0 ) { v . add ( new pair ( i , count ) ) ; } } if ( N > 2 ) v . add ( new pair ( N , 1 ) ) ; return v ; } static void printAnswer ( int n ) { Vector < pair > v = primeFactor ( n ) ; int maxi_size = 0 , prime_factor = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( maxi_size < v . get ( i ) . second ) { maxi_size = v . get ( i ) . second ; prime_factor = v . get ( i ) . first ; } } if ( maxi_size < 2 ) { System . out . print ( 1 << ' ▁ ' ) ; } else { int product = 1 ; System . out . print ( maxi_size + "NEW_LINE"); for ( int i = 0 ; i < maxi_size - 1 ; i ++ ) { System . out . print ( prime_factor + " ▁ " ) ; product *= prime_factor ; } System . out . print ( ( n / product ) ) ; } } public static void main ( String [ ] args ) { int N = 360 ; printAnswer ( N ) ; } }