Find largest factor of N such that N/F is less than K
Given two numbers N and K, the task is to find the minimum value X such that N < X*K.
Examples:
Input: N = 8, K = 7
Output: 2
Explanation:
Numbers less than K divisible by N are 1, 2 and 4.
So the minimum value of X is 2 such that 8 < 2*7 = 14.
Input: N = 999999733, K = 999999732
Output: 999999733
Explanation:
Since 999999733 is a prime number, so 999999733 is divisible by 1 and the number itself. Since K is less than 999999733.
So the minimum value of X is 999999733 such that 999999733 < 999999733*999999732.
Naive Approach: The given problem statement can be visualized as equation K * X = N. In this equation, the objective is to minimize X. So we have to find the maximum K which divides N. Below are the steps:
- Iterate over [1, K].
- Check for each number i such that (N % i) = 0. Keep updating the max variable that stores the maximum divisor of N traversed up to i.
- The required answer is N/max.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxValue( int N, int K)
{
int packages;
int maxi = 1;
for ( int i = 1; i <= K; i++) {
if (N % i == 0)
maxi = max(maxi, i);
}
packages = N / maxi;
cout << packages << endl;
}
int main()
{
int N = 8, K = 7;
findMaxValue(N, K);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
static void findMaxValue( int N, int K)
{
int packages;
int maxi = 1 ;
for ( int i = 1 ; i <= K; i++)
{
if (N % i == 0 )
maxi = Math.max(maxi, i);
}
packages = N / maxi;
System.out.println(packages);
}
public static void main (String[] args)
{
int N = 8 , K = 7 ;
findMaxValue(N, K);
}
}
|
Python3
def findMaxValue(N, K):
packages = 0 ;
maxi = 1 ;
for i in range ( 1 , K + 1 ):
if (N % i = = 0 ):
maxi = max (maxi, i);
packages = N / / maxi;
print (packages);
if __name__ = = '__main__' :
N = 8 ;
K = 7 ;
findMaxValue(N, K);
|
C#
using System;
class GFG{
static void findMaxValue( int N, int K)
{
int packages;
int maxi = 1;
for ( int i = 1; i <= K; i++)
{
if (N % i == 0)
maxi = Math.Max(maxi, i);
}
packages = N / maxi;
Console.WriteLine(packages);
}
public static void Main(String[] args)
{
int N = 8, K = 7;
findMaxValue(N, K);
}
}
|
Javascript
<script>
function findMaxValue(N, K)
{
let packages;
let maxi = 1;
for (let i = 1; i <= K; i++) {
if (N % i == 0)
maxi = Math.max(maxi, i);
}
packages = parseInt(N / maxi);
document.write(packages);
}
let N = 8, K = 7;
findMaxValue(N, K);
</script>
|
Time Complexity: O(K)
Auxiliary Space: O(1)
Efficient Approach: To optimize the above approach we will find the factor using the efficient approach discussed in this article. Below are the steps:
- Initialize the ans variable to store the largest factor of N.
- Iterate over [1, sqrt(N)] and do the following:
- Check if N is divisible by i or not.
- If not then check for the next number.
- Else if i ? K and N/i ? K then update ans to the maximum (i, N/i).
- The value of X will be N/ans.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int solve( int n, int k)
{
int ans = 0;
for ( int j = 1;
j * j <= n; j++) {
if (n % j == 0) {
if (j <= k) {
ans = max(ans, j);
}
if (n / j <= k) {
ans = max(ans, n / j);
}
}
}
return ans;
}
int main()
{
int N = 8, K = 7;
cout << (N / solve(N, K));
return 0;
}
|
Java
import java.util.*;
class GFG{
static int solve( int n, int k)
{
int ans = 0 ;
for ( int j = 1 ; j * j <= n; j++)
{
if (n % j == 0 )
{
if (j <= k)
{
ans = Math.max(ans, j);
}
if (n / j <= k)
{
ans = Math.max(ans, n / j);
}
}
}
return ans;
}
public static void main(String[] args)
{
int N = 8 , K = 7 ;
System.out.print((N / solve(N, K)));
}
}
|
Python3
def solve(n, k):
ans = 0 ;
for j in range ( 1 , n + 1 ):
if (j * j > n):
break ;
if (n % j = = 0 ):
if (j < = k):
ans = max (ans, j);
if (n / / j < = k):
ans = max (ans, n / / j);
return ans;
if __name__ = = '__main__' :
N = 8 ; K = 7 ;
print ((N / / solve(N, K)));
|
C#
using System;
class GFG{
static int solve( int n, int k)
{
int ans = 0;
for ( int j = 1; j * j <= n; j++)
{
if (n % j == 0)
{
if (j <= k)
{
ans = Math.Max(ans, j);
}
if (n / j <= k)
{
ans = Math.Max(ans, n / j);
}
}
}
return ans;
}
public static void Main(String[] args)
{
int N = 8, K = 7;
Console.Write((N / solve(N, K)));
}
}
|
Javascript
<script>
function solve(n, k)
{
let ans = 0;
for (let j = 1; j * j <= n; j++)
{
if (n % j == 0)
{
if (j <= k)
{
ans = Math.max(ans, j);
}
if (n / j <= k)
{
ans = Math.max(ans, n / j);
}
}
}
return ans;
}
let N = 8, K = 7;
document.write((N / solve(N, K)));
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...