Minimize difference between any multiple of given three numbers with K
Last Updated :
24 Dec, 2021
Given 4 integers A, B, C, and K the task is to find the minimum difference between any multiple of A, B, and C with K
Examples:
Input: A = 5, B = 4, C = 8, K = 9
output: 1
Explanation: Closest multiple of A, B and C greater than 9 is 10. Therefore, minimum difference is 10-9 = 1
Input: A = 6, B = 10, C = 9, K = 2
Output: 4
Explanation: Closest multiple of A, B and C greater than 2 is 6. Therefore, minimum difference is 6-2 = 4
Naive Approach: The task can be solved by using a for-loop to get the next multiples of A, B, and C, by keeping track of the closest multiple just greater than K Follow the below steps to solve the problem:
- Initialize 3 boolean variables say fa, fb, and fc to denote whether the multiples of A, B, and C becomes greater than K
- Start multiplying A, B, and C with cur initialized with 1
- As soon as, one of the three numbers becomes greater than K, store the difference between the nearest multiple and K accordingly
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int A, int B, int C, int K)
{
int ans = INT_MAX;
bool fa = false , fb = false , fc = false ;
int cur = 1;
while (1) {
A *= cur;
B *= cur;
C *= cur;
if (fa && fb && fb)
break ;
if (!fa) {
if (A >= K) {
ans = min(ans, A - K);
fa = true ;
}
}
if (!fb) {
if (B >= K) {
ans = min(ans, B - K);
fb = true ;
}
}
if (!fc) {
if (C >= K) {
ans = min(ans, C - K);
fc = true ;
}
}
cur++;
}
cout << ans << endl;
}
int main()
{
int A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
return 0;
}
|
Java
class GFG{
static void solve( int A, int B, int C, int K)
{
int ans = Integer.MAX_VALUE;
boolean fa = false , fb = false , fc = false ;
int cur = 1 ;
while ( true ) {
A *= cur;
B *= cur;
C *= cur;
if (fa && fb && fb)
break ;
if (!fa) {
if (A >= K) {
ans = Math.min(ans, A - K);
fa = true ;
}
}
if (!fb) {
if (B >= K) {
ans = Math.min(ans, B - K);
fb = true ;
}
}
if (!fc) {
if (C >= K) {
ans = Math.min(ans, C - K);
fc = true ;
}
}
cur++;
}
System.out.print(ans + "\n" );
}
public static void main(String[] args)
{
int A = 6 , B = 10 , C = 9 , K = 2 ;
solve(A, B, C, K);
}
}
|
Python3
def solve(A, B, C, K):
ans = 10 * * 9
fa = False
fb = False
fc = False
cur = 1
while ( 1 ):
A * = cur
B * = cur
C * = cur
if (fa and fb and fb):
break
if ( not fa):
if (A > = K):
ans = min (ans, A - K)
fa = True
if ( not fb):
if (B > = K):
ans = min (ans, B - K)
fb = True
if ( not fc):
if (C > = K):
ans = min (ans, C - K)
fc = True
cur + = 1
print (ans)
A = 6
B = 10
C = 9
K = 2
solve(A, B, C, K)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static void solve( int A, int B, int C, int K)
{
int ans = Int32.MaxValue;
bool fa = false , fb = false , fc = false ;
int cur = 1;
while ( true ) {
A *= cur;
B *= cur;
C *= cur;
if (fa && fb && fb)
break ;
if (!fa) {
if (A >= K) {
ans = Math.Min(ans, A - K);
fa = true ;
}
}
if (!fb) {
if (B >= K) {
ans = Math.Min(ans, B - K);
fb = true ;
}
}
if (!fc) {
if (C >= K) {
ans = Math.Min(ans, C - K);
fc = true ;
}
}
cur++;
}
Console.Write(ans + "\n" );
}
public static void Main()
{
int A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
}
}
|
Javascript
<script>
function solve(A, B, C, K) {
let ans = Number.MAX_VALUE;
let fa = false , fb = false , fc = false ;
let cur = 1;
while (1) {
A *= cur;
B *= cur;
C *= cur;
if (fa && fb && fb)
break ;
if (!fa) {
if (A >= K) {
ans = Math.min(ans, A - K);
fa = true ;
}
}
if (!fb) {
if (B >= K) {
ans = Math.min(ans, B - K);
fb = true ;
}
}
if (!fc) {
if (C >= K) {
ans = Math.min(ans, C - K);
fc = true ;
}
}
cur++;
}
document.write(ans + '<br>' );
}
let A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
</script>
|
Time Complexity: O(min(K/A, K/B, K/C))
Auxiliary Space: O(1)
Efficient Approach: Above approach can be generalized into a formula: min(⌈K/A⌉*A, ⌈K/B⌉*B, ⌈K/C⌉*C) − K.
- ⌈K/A⌉*A gives the nearest multiple of A just greater than K
- ⌈K/B⌉*B gives the nearest multiple of B just greater than K
- ⌈K/C⌉*C gives the nearest multiple of C just greater than K
- Difference of minimum of all these with K gives the desired result
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int A, int B, int C, int K)
{
int fa, fb, fc;
if ((K % A) != 0) {
fa = ((K / A) + 1) * A;
}
else {
fa = (K / A) * A;
}
if ((K % B) != 0) {
fb = ((K / B) + 1) * B;
}
else {
fb = (K / B) * B;
}
if ((K % C) != 0) {
fc = ((K / C) + 1) * C;
}
else {
fc = (K / C) * C;
}
int ans;
ans = min(fa - K, min(fc - K, fb - K));
cout << ans << endl;
}
int main()
{
int A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
return 0;
}
|
Java
public class GFG {
static void solve( int A, int B, int C, int K)
{
int fa, fb, fc;
if ((K % A) != 0 ) {
fa = ((K / A) + 1 ) * A;
}
else {
fa = (K / A) * A;
}
if ((K % B) != 0 ) {
fb = ((K / B) + 1 ) * B;
}
else {
fb = (K / B) * B;
}
if ((K % C) != 0 ) {
fc = ((K / C) + 1 ) * C;
}
else {
fc = (K / C) * C;
}
int ans;
ans = Math.min(fa - K, Math.min(fc - K, fb - K));
System.out.println(ans);
}
public static void main (String[] args)
{
int A = 6 , B = 10 , C = 9 , K = 2 ;
solve(A, B, C, K);
}
}
|
Python3
def solve( A, B, C, K) :
if ((K % A) ! = 0 ) :
fa = ((K / / A) + 1 ) * A;
else :
fa = (K / / A) * A;
if ((K % B) ! = 0 ) :
fb = ((K / / B) + 1 ) * B;
else :
fb = (K / / B) * B;
if ((K % C) ! = 0 ) :
fc = ((K / / C) + 1 ) * C;
else :
fc = (K / / C) * C;
ans = min (fa - K, min (fc - K, fb - K));
print (ans);
if __name__ = = "__main__" :
A = 6 ; B = 10 ; C = 9 ; K = 2 ;
solve(A, B, C, K);
|
C#
using System;
public class GFG {
static void solve( int A, int B, int C, int K)
{
int fa, fb, fc;
if ((K % A) != 0) {
fa = ((K / A) + 1) * A;
}
else {
fa = (K / A) * A;
}
if ((K % B) != 0) {
fb = ((K / B) + 1) * B;
}
else {
fb = (K / B) * B;
}
if ((K % C) != 0) {
fc = ((K / C) + 1) * C;
}
else {
fc = (K / C) * C;
}
int ans;
ans = Math.Min(fa - K, Math.Min(fc - K, fb - K));
Console.WriteLine(ans);
}
public static void Main( string [] args)
{
int A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
}
}
|
Javascript
<script>
function solve(A , B , C , K)
{
var fa, fb, fc;
if ((K % A) != 0) {
fa = parseInt((K / A) + 1) * A;
}
else {
fa = parseInt(K / A) * A;
}
if ((K % B) != 0) {
fb = parseInt((K / B) + 1) * B;
}
else {
fb = parseInt(K / B) * B;
}
if ((K % C) != 0) {
fc = parseInt((K / C) + 1) * C;
}
else {
fc = parseInt(K / C) * C;
}
var ans;
ans = Math.min(fa - K, Math.min(fc - K, fb - K));
document.write(ans);
}
var A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...