Given a positive integer N, the task is to generate a sequence say arr[] of maximum length having all elements at least 2 such that the product of all the numbers in the sequence is N and for any pair of indices (i, j) and i < j, arr[j] is divisible by arr[i].
Examples:
Input: N = 360
Output: Maximum size = 3, final array = {2, 2, 90}
Explanation:
Consider the array arr[] be the resultant array, then the following operations can be performed:
- Add 2 to the array arr[]. Now, N = 360/2 = 180 and arr[] = {2}.
- Add 2 to the array arr[]. Now, N = 180/2 = 90 and arr[] = {2, 2}.
- Add 90 to the array arr[]. Now, arr[] = {2, 2, 90}.
After the above operations, the maximum size of the array is 3 and the resultant array is {2, 2, 90}.
Input: N = 810
Output: Maximum size = 4, final array = {3, 3, 3, 30}
Approach: The given problem can be solved by using the concept of prime factorization, the idea is to find the prime number having the maximum frequency of occurrence as N that can be represented as the product of prime numbers as:
N = (a1p1)*(a2p1)*(a3p1)*(a4p1)(……)*(anpn)
Follow the steps below to solve the problem:
- Initialize a Map, say M that stores all the prime numbers as a key and their powers as values. For example, for the value 23, the map M stores as M[2] = 3.
- Choose the prime factor which has the maximum power factor and store that power in a variable, say ans, and store that prime number in a variable say P.
- If the value of ans is smaller than 2, then the resultant array will be of size 1 and the array element will be equal to N.
- Otherwise, print the value of ans as the maximum length, and to print the final sequence, print the value of P (ans – 1) number of times, and then print (N/2ans) at the end.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector<pair< int , int > > primeFactor(
int N)
{
vector<pair< int , int > > v;
int count = 0;
while (!(N % 2)) {
N >>= 1;
count++;
}
if (count)
v.push_back({ 2, count });
for ( int i = 3;
i <= sqrt (N); i += 2) {
count = 0;
while (N % i == 0) {
count++;
N = N / i;
}
if (count) {
v.push_back({ i, count });
}
}
if (N > 2)
v.push_back({ N, 1 });
return v;
}
void printAnswer( int n)
{
vector<pair< int , int > > v
= primeFactor(n);
int maxi_size = 0, prime_factor = 0;
for ( int i = 0; i < v.size(); i++) {
if (maxi_size < v[i].second) {
maxi_size = v[i].second;
prime_factor = v[i].first;
}
}
if (maxi_size < 2) {
cout << 1 << ' ' << n;
}
else {
int product = 1;
cout << maxi_size << endl;
for ( int i = 0;
i < maxi_size - 1; i++) {
cout << prime_factor << " " ;
product *= prime_factor;
}
cout << (n / product);
}
}
int main()
{
int N = 360;
printAnswer(N);
return 0;
}
|
Java
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 + "\n" );
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);
}
}
|
Python3
from math import sqrt
def primeFactor(N):
v = []
count = 0
while ((N % 2 ) = = 0 ):
N >> = 1
count + = 1
if (count):
v.append([ 2 , count])
for i in range ( 3 , int (sqrt(N)) + 1 , 2 ):
count = 0
while (N % i = = 0 ):
count + = 1
N = N / i
if (count):
v.append([i, count])
if (N > 2 ):
v.append([N, 1 ])
return v
def printAnswer(n):
v = primeFactor(n)
maxi_size = 0
prime_factor = 0
for i in range ( len (v)):
if (maxi_size < v[i][ 1 ]):
maxi_size = v[i][ 1 ]
prime_factor = v[i][ 0 ]
if (maxi_size < 2 ):
print ( 1 , n)
else :
product = 1
print (maxi_size)
for i in range (maxi_size - 1 ):
print (prime_factor, end = " " )
product * = prime_factor
print (n / / product)
if __name__ = = '__main__' :
N = 360
printAnswer(N)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class pair
{
public int first;
public int second;
public pair( int first, int second)
{
this .first = first;
this .second = second;
}
}
static List<pair > primeFactor(
int N)
{
List<pair > v = new List<pair>();
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)
{
List<pair > v
= primeFactor(n);
int maxi_size = 0, prime_factor = 0;
for ( int i = 0; i < v.Count; i++) {
if (maxi_size < v[i].second) {
maxi_size = v[i].second;
prime_factor = v[i].first;
}
}
if (maxi_size < 2) {
Console.Write(1 << ' ' );
}
else {
int product = 1;
Console.Write(maxi_size + "\n" );
for ( int i = 0;
i < maxi_size - 1; i++) {
Console.Write(prime_factor+ " " );
product *= prime_factor;
}
Console.Write((n / product));
}
}
public static void Main(String[] args)
{
int N = 360;
printAnswer(N);
}
}
|
Javascript
<script>
function primeFactor(N)
{
let v = [];
let count = 0;
while (!(N % 2)) {
N >>= 1;
count++;
}
if (count)
v.push([2, count]);
for (let i = 3; i <= Math.sqrt(N); i += 2) {
count = 0;
while (N % i == 0) {
count++;
N = Math.floor(N / i);
}
if (count) {
v.push([i, count]);
}
}
if (N > 2)
v.push([N, 1]);
return v;
}
function printAnswer(n)
{
let v = primeFactor(n);
let maxi_size = 0, prime_factor = 0;
for (let i = 0; i < v.length; i++) {
if (maxi_size < v[i][1]) {
maxi_size = v[i][1];
prime_factor = v[i][0];
}
}
if (maxi_size < 2) {
document.write(1 + ' ' + n);
}
else {
let product = 1;
document.write(maxi_size + "<br>" );
for (let i = 0; i < maxi_size - 1; i++) {
document.write(prime_factor + " " );
product *= prime_factor;
}
document.write(Math.floor((n / product)));
}
}
let N = 360;
printAnswer(N);
</script>
|
Time Complexity: O(N3/2)
Auxiliary Space: O(N)