Construct longest Array starting with N and A[i] as multiple of A[i+1]
Given an integer N, the task is to construct the longest possible array A[], such that the following conditions hold:
- A[0] = N.
- No two adjacent elements should be equal.
- For all i (0 < i < array length), such that A[i] is divisible by A[i + 1]
Note: If there are many possible sequence, print any sequence.
Examples:
Input: N = 10
Output: 3, {10, 2, 1}
Explanation: The maximum possible length of the array A[] is 3
which is {10, 2, 1}, Thus no bigger array is possible for N = 10.
Input: N = 8
Output: 4, {8, 4, 2, 1}
Approach: The Intuition to solve this problem and to maximize the sequence length is:
For each element simply find the highest divisor (apart from the number itself) of the previous number which in return will have maximum number of divisors possible.
Follow the illustration below for a better understanding.
Illustration:
Consider N = 8;
- N = 8, highest divisor = 4
- N = 4, highest divisor = 2
- N = 2, highest divisor = 1
- Here, the base condition is reached thus, stop.
Following are the steps to implement above approach:
- Run a loop till N is greater than 1
- In the last push 1 to the resultant array.
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
vector< int > getMaximumSequence( int & N)
{
vector< int > sequence;
if (N == 1) {
sequence.push_back(1);
return sequence;
}
else {
while (N > 1) {
sequence.push_back(N);
int maxx = 1;
vector< int > ds;
ds.push_back(1);
for ( int i = 2; i <= sqrt (N);
i++) {
if (N % i == 0) {
ds.push_back(i);
ds.push_back(N / i);
}
}
N = *max_element(ds.begin(),
ds.end());
}
sequence.push_back(N);
return sequence;
}
}
void printSequence(vector< int >& res)
{
cout << res.size() << "\n" ;
for ( auto x : res) {
cout << x << " " ;
}
}
int main()
{
int N = 8;
vector< int > res = getMaximumSequence(N);
printSequence(res);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static ArrayList<Integer>
getMaximumSequence( int N)
{
ArrayList<Integer> sequence
= new ArrayList<Integer>();
if (N == 1 ) {
sequence.add( 1 );
return sequence;
}
else {
while (N > 1 ) {
sequence.add(N);
int maxx = 1 ;
ArrayList<Integer> ds
= new ArrayList<Integer>();
ds.add( 1 );
for ( int i = 2 ; i <= Math.sqrt(N); i++) {
if (N % i == 0 ) {
ds.add(i);
ds.add(N / i);
}
}
N = Collections.max(ds);
}
sequence.add(N);
return sequence;
}
}
public static void printSequence(ArrayList<Integer> res)
{
System.out.println(res.size());
for ( int x : res) {
System.out.print(x + " " );
}
}
public static void main(String[] args)
{
int N = 8 ;
ArrayList<Integer> res = getMaximumSequence(N);
printSequence(res);
}
}
|
Python3
def getMaximumSequence(N):
sequence = []
if N = = 1 :
sequence.append( 1 )
return sequence
else :
while N > 1 :
sequence.append(N)
maxx = 1
ds = []
ds.append( 1 )
for i in range ( 2 , 1 + int (N * * 0.5 )):
if N % i = = 0 :
ds.append(i)
ds.append(N / / i)
N = max (ds)
sequence.append(N)
return sequence
def printSequence(res):
print ( len (res))
print ( " " .join( list ( map ( str , res))))
N = 8
res = getMaximumSequence(N)
printSequence(res)
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
public class GFG
{
public static List< int > getMaximumSequence( int N)
{
List< int > sequence = new List< int >();
if (N == 1) {
sequence.Add(1);
return sequence;
}
else {
while (N > 1) {
sequence.Add(N);
List< int > ds = new List< int >();
ds.Add(1);
for ( int i = 2; i <= Math.Sqrt(N); i++) {
if (N % i == 0) {
ds.Add(i);
ds.Add(N / i);
}
}
N = ds.Max();
}
sequence.Add(N);
return sequence;
}
}
public static void printSequence(List< int > res)
{
Console.WriteLine(res.Count);
for ( int x = 0; x < res.Count; x++) {
Console.Write(res[x] + " " );
}
}
public static void Main( string [] args)
{
int N = 8;
List< int > res = getMaximumSequence(N);
printSequence(res);
}
}
|
Javascript
<script>
function getMaximumSequence(N) {
let sequence = [];
if (N == 1) {
sequence.push(1);
return sequence;
}
else {
while (N > 1) {
sequence.push(N);
let maxx = 1;
let ds = [];
ds.push(1);
for (let i = 2; i <= Math.sqrt(N);
i++) {
if (N % i == 0) {
ds.push(i);
ds.push(Math.floor(N / i));
}
}
N = Math.max(...ds);
}
sequence.push(N);
return sequence;
}
}
function printSequence(res) {
document.write(res.length + '<br>' );
for (let x of res) {
document.write(x + " " )
}
}
let N = 8;
let res = getMaximumSequence(N);
printSequence(res);
</script>
|
Time Complexity: O(log2N * Sqrt(M))
Auxiliary Space: O(log2N * M), where M is number of divisors and logN is the number of the times loop runs
Last Updated :
21 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...