Maximizing GCD by Partitioning a Number into K Parts
Last Updated :
16 Mar, 2024
Given a number N, break the number into K positive integers a1, a2, a3 …. aK such that a1 + a2 + a3 …. aK = N. The task is to maximize the GCD(a1, a2, a3 …. aK).
Examples:
Input: N = 420, K = 69
Output: 6
Explanation: We can break 420 into 68 integers of value 6 and 1 integer of value 12. Therefore, the max GCD is 6.
Input: N = 5, K = 5
Output: 1
Explanation: We can break 5 into 5 integers of value 1. Therefore, the max GCD is 1.
Approach: To solve the problem, follow the below idea:
We can use the GCD (Greatest Common Divisor) property and focus on finding the largest factor “d” of N which is common among all the K integers. The problem can be solved by finding all the factors of N, say D and check if we can break N in such a manner that all the K positive integers are multiples of D. Among all the factors, return the maximum among all them.
Step-by-step algorithm:
- Find all the factors of N by running a loop till sqrt(N).
- Inside the loop, check if we can have all the K positive integers as a multiple of this factor.
- Among all the factors, return the maximum.
Below is the implementation of the algorithm:
Java
import java.util.Scanner;
public class Main {
// Defining constants
static final int MOD = 1000000007;
// Function to solve the problem
static int solve(long N, long K) {
// Initializing a variable to store the result
int S = (int) N;
// Looping through potential divisors up to sqrt(N)
for (int i = 1; i * i <= N; i++) {
// Checking if i is a divisor of x
if (N % i == 0) {
int parts = (int) (N / i);
// Checking if x/i is greater than or equal to n
if (parts >= K) {
// Updating s with the minimum value between
// x/i and the current value of s
if (parts < S) {
S = parts;
}
}
parts = i;
// Checking if i is greater than or equal to n
if (parts >= K) {
// Updating S with the minimum value between
// i and the current value of S
if (parts < S) {
S = parts;
}
}
}
}
// Return the result
return (int) (N / S);
}
// Main function
public static void main(String[] args) {
// Taking input for N and K
long N = 420, K = 69;
System.out.println(solve(N, K));
}
}
// This code is contributed by shivamgupta0987654321
Python
import math
# Function to solve the problem
def solve(N, K):
# Initializing a variable to store the result
S = N
# Looping through potential divisors up to sqrt(N)
for i in range(1, int(math.sqrt(N)) + 1):
# Checking if i is a divisor of N
if N % i == 0:
parts = N // i
# Checking if N/i is greater than or equal to K
if parts >= K:
# Updating S with the minimum value between N/i and the current value of S
S = min(S, parts)
parts = i
# Checking if i is greater than or equal to K
if parts >= K:
# Updating S with the minimum value between i and the current value of S
S = min(S, parts)
# Return the result
return N // S
# Main function
def main():
# Taking input for N and K
N = 420
K = 69
print(solve(N, K))
if __name__ == "__main__":
main()
C#
using System;
class Program {
// Function to solve the problem
static int Solve(long N, long K)
{
// Initializing a variable to store the result
int S = (int)N;
// Looping through potential divisors up to sqrt(N)
for (long i = 1; i * i <= N; i++) {
// Checking if i is a divisor of N
if (N % i == 0) {
long parts = N / i;
// Checking if N/i is greater than or equal
// to K
if (parts >= K) {
// Updating S with the minimum value
// between N/i and the current value of
// S
if (parts < S) {
S = (int)parts;
}
}
parts = i;
// Checking if i is greater than or equal to
// K
if (parts >= K) {
// Updating S with the minimum value
// between i and the current value of S
if (parts < S) {
S = (int)parts;
}
}
}
}
// Return the result
return (int)(N / S);
}
// Main function
static void Main(string[] args)
{
// Taking input for N and K
long N = 420, K = 69;
Console.WriteLine(Solve(N, K));
}
}
Javascript
// Function to solve the problem
function solve(N, K) {
// Initializing a variable to store the result
let S = N;
// Looping through potential divisors up to sqrt(N)
for (let i = 1; i * i <= N; i++) {
// Checking if i is a divisor of N
if (N % i === 0) {
let parts = Math.floor(N / i);
// Checking if N/i is greater than or equal to K
if (parts >= K) {
// Updating S with the minimum value between
// N/i and the current value of S
if (parts < S) {
S = parts;
}
}
parts = i;
// Checking if i is greater than or equal to K
if (parts >= K) {
// Updating S with the minimum value between
// i and the current value of S
if (parts < S) {
S = parts;
}
}
}
}
// Return the result
return Math.floor(N / S);
}
// Main function
function main() {
// Taking input for N and K
const N = 420, K = 69;
console.log(solve(N, K));
}
// Calling the main function to execute the code
main();
C++14
#include <bits/stdc++.h>
using namespace std;
// Defining constants
#define MOD 1000000007
// Function to solve the problem
int solve(long long N, long long K)
{
// Initializing a variable to store the result
int S = N;
// Looping through potential divisors up to sqrt(N)
for (int i = 1; i * i <= N; i++) {
// Checking if i is a divisor of x
if (N % i == 0) {
int parts = N / i;
// Checking if x/i is greater than or equal to n
if (parts >= K) {
// Updating s with the minimum value between
// x/i and the current value of s
if (parts < S) {
S = parts;
}
}
parts = i;
// Checking if i is greater than or equal to n
if (parts >= K) {
// Updating S with the minimum value between
// i and the current value of S
if (parts < S) {
S = parts;
}
}
}
}
// Return the result
return N / S;
}
// Main function
signed main()
{
// Taking input for N and K
long long int N = 420, K = 69;
cout << solve(N, K);
}
Time Complexity: O(sqrt(N)), where N is the input number which needs to be broken in K positive integers.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...