Series with largest GCD and sum equals to n
Last Updated :
18 Sep, 2023
Given an integer n, print m increasing numbers such that the sum of m numbers is equal to n and the GCD of m numbers is maximum among all series possible. If no series is possible then print “-1”.
Examples :
Input : n = 24,
m = 3
Output : 4 8 12
Explanation : (3, 6, 15) is also a series
of m numbers which sums to N, but gcd = 3
(4, 8, 12) has gcd = 4 which is the maximum
possible.
Input : n = 6
m = 4
Output : -1
Explanation: It is not possible as the
least GCD sequence will be 1+2+3+4 which
is greater than n, hence print -1.
Approach:
The most common observation is that the gcd of the series will always be a divisor of n. The maximum gcd possible (say b) will be n/sum, where sum is the sum of 1+2+..m.
If b turns out to be 0, then the sum of 1+2+3..+k exceeds n which is invalid, hence output “-1”.
Traverse to find out all the divisors possible, a loop till sqrt(n). If the current divisor is i, the best possible way to take the series will be to consider i, 2*i, 3*i, …(m-1)*i, and their sum is s which is equal to i * (m*(m-1))/2 . The last number will be n-s.
Along with i being the divisor, n/i will be the other divisor so check for that also.
Take maximum of possible divisor possible (say r) which should be less than or equals to b and print the sequence as r, 2*r, … (m-1)*r, n—s.
If no such divisors are found simply output “-1”.
C++
#include <bits/stdc++.h>
using namespace std;
void print_sequence( int n, int k)
{
int b = n / (k * (k + 1) / 2);
if (b == 0) {
cout << -1 << endl;
} else {
int r = 1;
for ( int x = 1; x * x <= n; x++) {
if (n % x != 0)
continue ;
if (x <= b && x > r)
r = x;
if (n / x <= b && n / x > r)
r = n / x;
}
for ( int i = 1; i < k; i++)
cout << r * i << " " ;
int res = n - (r * (k * (k - 1) / 2));
cout << res << endl;
}
}
int main()
{
int n = 24;
int k = 4;
print_sequence(n, k);
n = 24, k = 5;
print_sequence(n, k);
n = 6, k = 4;
print_sequence(n, k);
}
|
Java
import java.io.*;
class GFG {
static void print_sequence( int n, int k)
{
int b = n / (k * (k + 1 ) / 2 );
if (b == 0 ) {
System.out.println( "-1" );
} else {
int r = 1 ;
for ( int x = 1 ; x * x <= n; x++) {
if (n % x != 0 )
continue ;
if (x <= b && x > r)
r = x;
if (n / x <= b && n / x > r)
r = n / x;
}
for ( int i = 1 ; i < k; i++)
System.out.print(r * i + " " );
int res = n - (r * (k * (k - 1 ) / 2 ));
System.out.println(res);
}
}
public static void main(String[] args)
{
int n = 24 ;
int k = 4 ;
print_sequence(n, k);
n = 24 ; k = 5 ;
print_sequence(n, k);
n = 6 ; k = 4 ;
print_sequence(n, k);
}
}
|
Python3
def print_sequence(n, k):
b = int (n / (k * (k + 1 ) / 2 ));
if b = = 0 :
print ( "-1" )
else :
r = 1 ;
x = 1
while x * * 2 < = n:
if n % x ! = 0 :
continue ;
elif x < = b and x > r:
r = x
elif n / x < = b and n / x > r :
r = n / x
x = x + 1
i = 1
while i < k :
print (r * i, end = " " )
i = i + 1
last_term = n - (r * (k * (k - 1 ) / 2 ))
print (last_term)
print_sequence( 24 , 4 )
print_sequence( 24 , 5 )
print_sequence( 6 , 4 )
|
C#
using System;
class GFG {
static void print_sequence( int n, int k)
{
int b = n / (k * (k + 1) / 2);
if (b == 0)
{
Console.Write( "-1" );
}
else
{
int r = 1;
for ( int x = 1; x * x <= n; x++)
{
if (n % x != 0)
continue ;
if (x <= b && x > r)
r = x;
if (n / x <= b && n / x > r)
r = n / x;
}
for ( int i = 1; i < k; i++)
Console.Write(r * i + " " );
int res = n - (r * (k *
(k - 1) / 2));
Console.WriteLine(res);
}
}
public static void Main()
{
int n = 24;
int k = 4;
print_sequence(n, k);
n = 24; k = 5;
print_sequence(n, k);
n = 6; k = 4;
print_sequence(n, k);
}
}
|
PHP
<?php
function print_sequence( $n , $k )
{
$b = (int)( $n / ( $k * ( $k + 1) / 2));
if ( $b == 0)
{
echo (-1);
}
else
{
$r = 1;
for ( $x = 1; $x * $x <= $n ; $x ++)
{
if ( $n % $x != 0)
continue ;
if ( $x <= $b && $x > $r )
$r = $x ;
if ( $n / $x <= $b && $n / $x > $r )
$r = $n / $x ;
}
for ( $i = 1; $i < $k ; $i ++)
echo ( $r * $i . " " );
$res = $n - ( $r * ( $k * ( $k - 1) / 2));
echo ( $res . "\n" );
}
}
$n = 24;
$k = 4;
print_sequence( $n , $k );
$n = 24; $k = 5;
print_sequence( $n , $k );
$n = 6; $k = 4;
print_sequence( $n , $k );
?>
|
Javascript
<script>
function print_sequence(n, k)
{
let b = parseInt(n / (k * (k + 1) / 2));
if (b == 0)
{
document.write(-1);
}
else
{
let r = 1;
for (let x = 1; x * x <= n; x++)
{
if (n % x != 0)
continue ;
if (x <= b && x > r)
r = x;
if (n / x <= b && n / x > r)
r = n / x;
}
for (let i = 1; i < k; i++)
document.write(r * i + " " );
let res = n - (r * (k * (k - 1) / 2));
document.write(res + "<br>" );
}
}
let n = 24;
let k = 4;
print_sequence(n, k);
n = 24;
k = 5;
print_sequence(n, k);
n = 6;
k = 4;
print_sequence(n, k);
</script>
|
Output :
2 4 6 12
1 2 3 4 14
-1
Time complexity: O( sqrt (n) )
Auxiliary Space: O(1)