Minimize difference between any multiple of given three numbers with K - GeeksforGeeks

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 = 9output: 1Explanation: 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 = 2Output: 4Explanation: 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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the difference between// closest multiple of A, B, C with Kvoid solve(int A, int B, int C, int K){     // Stores the minimum difference    int ans = INT_MAX;     // Check whether multiples of A, B and C becomes    // greater than K    bool fa = false, fb = false, fc = false;     // Start multiplication from 1    int cur = 1;    while (1) {         // Finding multiples        A *= cur;        B *= cur;        C *= cur;         // All the multiples becomes        // greater than K        if (fa && fb && fb)            break;         // Multiple of A        if (!fa) {             // Valid multiple            if (A >= K) {                 // Minimize ans                ans = min(ans, A - K);                fa = true;            }        }         // Multiple of B        if (!fb) {             // Valid multiple            if (B >= K) {                 // Minimize ans                ans = min(ans, B - K);                fb = true;            }        }         // Multiple of C        if (!fc) {             // Valid multiple            if (C >= K) {                 // Minimize ans                ans = min(ans, C - K);                fc = true;            }        }         cur++;    }     // Resultant answer    cout << ans << endl;} // Driver Codeint main(){    int A = 6, B = 10, C = 9, K = 2;    solve(A, B, C, K);    return 0;}
// Java program for the above approachclass GFG{ // Function to find the difference between// closest multiple of A, B, C with Kstatic void solve(int A, int B, int C, int K){     // Stores the minimum difference    int ans = Integer.MAX_VALUE;     // Check whether multiples of A, B and C becomes    // greater than K    boolean fa = false, fb = false, fc = false;     // Start multiplication from 1    int cur = 1;    while (true) {         // Finding multiples        A *= cur;        B *= cur;        C *= cur;         // All the multiples becomes        // greater than K        if (fa && fb && fb)            break;         // Multiple of A        if (!fa) {             // Valid multiple            if (A >= K) {                 // Minimize ans                ans = Math.min(ans, A - K);                fa = true;            }        }         // Multiple of B        if (!fb) {             // Valid multiple            if (B >= K) {                 // Minimize ans                ans = Math.min(ans, B - K);                fb = true;            }        }         // Multiple of C        if (!fc) {             // Valid multiple            if (C >= K) {                 // Minimize ans                ans = Math.min(ans, C - K);                fc = true;            }        }         cur++;    }     // Resultant answer    System.out.print(ans +"\n");} // Driver Codepublic static void main(String[] args){    int A = 6, B = 10, C = 9, K = 2;    solve(A, B, C, K);}} // This code is contributed by shikhasingrajput
# Python Program to implement# the above approach # Function to find the difference between# closest multiple of A, B, C with Kdef solve(A, B, C, K):     # Stores the minimum difference    ans = 10 ** 9     # Check whether multiples of A, B and C becomes    # greater than K    fa = False    fb = False    fc = False     # Start multiplication from 1    cur = 1    while (1):         # Finding multiples        A *= cur        B *= cur        C *= cur         # All the multiples becomes        # greater than K        if (fa and fb and fb):            break         # Multiple of A        if (not fa):             # Valid multiple            if (A >= K):                 # Minimize ans                ans = min(ans, A - K)                fa = True         # Multiple of B        if (not fb):             # Valid multiple            if (B >= K):                 # Minimize ans                ans = min(ans, B - K)                fb = True         # Multiple of C        if (not fc):             # Valid multiple            if (C >= K):                 # Minimize ans                ans = min(ans, C - K)                fc = True         cur += 1     # Resultant answer    print(ans) # Driver CodeA = 6B = 10C = 9K = 2solve(A, B, C, K)  # This code is contributed by saurabh_jaiswal.
// C# program for the above approachusing System;using System.Collections.Generic; class GFG { // Function to find the difference between// closest multiple of A, B, C with Kstatic void solve(int A, int B, int C, int K){     // Stores the minimum difference    int ans = Int32.MaxValue;     // Check whether multiples of A, B and C becomes    // greater than K    bool fa = false, fb = false, fc = false;     // Start multiplication from 1    int cur = 1;    while (true) {         // Finding multiples        A *= cur;        B *= cur;        C *= cur;         // All the multiples becomes        // greater than K        if (fa && fb && fb)            break;         // Multiple of A        if (!fa) {             // Valid multiple            if (A >= K) {                 // Minimize ans                ans = Math.Min(ans, A - K);                fa = true;            }        }         // Multiple of B        if (!fb) {             // Valid multiple            if (B >= K) {                 // Minimize ans                ans = Math.Min(ans, B - K);                fb = true;            }        }         // Multiple of C        if (!fc) {             // Valid multiple            if (C >= K) {                 // Minimize ans                ans = Math.Min(ans, C - K);                fc = true;            }        }         cur++;    }     // Resultant answer    Console.Write(ans +"\n");}     // Driver Code    public static void Main()    {        int A = 6, B = 10, C = 9, K = 2;        solve(A, B, C, K);    }} // This code is contributed by sanjoy_62.
<script>        // JavaScript Program to implement        // the above approach         // Function to find the difference between        // closest multiple of A, B, C with K        function solve(A, B, C, K) {             // Stores the minimum difference            let ans = Number.MAX_VALUE;             // Check whether multiples of A, B and C becomes            // greater than K            let fa = false, fb = false, fc = false;             // Start multiplication from 1            let cur = 1;            while (1) {                 // Finding multiples                A *= cur;                B *= cur;                C *= cur;                 // All the multiples becomes                // greater than K                if (fa && fb && fb)                    break;                 // Multiple of A                if (!fa) {                     // Valid multiple                    if (A >= K) {                         // Minimize ans                        ans = Math.min(ans, A - K);                        fa = true;                    }                }                 // Multiple of B                if (!fb) {                     // Valid multiple                    if (B >= K) {                         // Minimize ans                        ans = Math.min(ans, B - K);                        fb = true;                    }                }                 // Multiple of C                if (!fc) {                     // Valid multiple                    if (C >= K) {                         // Minimize ans                        ans = Math.min(ans, C - K);                        fc = true;                    }                }                 cur++;            }             // Resultant answer            document.write(ans + '<br>');        }         // Driver Code         let A = 6, B = 10, C = 9, K = 2;        solve(A, B, C, K);     // This code is contributed by Potta Lokesh    </script>
  
4
 
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++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find the difference between// closest multiple of A, B, C with Kvoid solve(int A, int B, int C, int K){    // Stores the multiples of A, B, C    // just greater than K    int fa, fb, fc;     // Multiple of A    if ((K % A) != 0) {         fa = ((K / A) + 1) * A;    }    else {        fa = (K / A) * A;    }     // Multiple of B    if ((K % B) != 0) {         fb = ((K / B) + 1) * B;    }    else {        fb = (K / B) * B;    }     // Multiple of C    if ((K % C) != 0) {         fc = ((K / C) + 1) * C;    }    else {        fc = (K / C) * C;    }     // Store the resultant answer    int ans;    ans = min(fa - K, min(fc - K, fb - K));    cout << ans << endl;} // Drive codeint main(){    int A = 6, B = 10, C = 9, K = 2;    solve(A, B, C, K);    return 0;}
// Java program for the above approachpublic class GFG {         // Function to find the difference between    // closest multiple of A, B, C with K    static void solve(int A, int B, int C, int K)    {               // Stores the multiples of A, B, C        // just greater than K        int fa, fb, fc;             // Multiple of A        if ((K % A) != 0) {                 fa = ((K / A) + 1) * A;        }        else {            fa = (K / A) * A;        }             // Multiple of B        if ((K % B) != 0) {                 fb = ((K / B) + 1) * B;        }        else {            fb = (K / B) * B;        }             // Multiple of C        if ((K % C) != 0) {                 fc = ((K / C) + 1) * C;        }        else {            fc = (K / C) * C;        }             // Store the resultant answer        int ans;        ans = Math.min(fa - K, Math.min(fc - K, fb - K));        System.out.println(ans);    }     // Drive code    public static void main (String[] args)    {        int A = 6, B = 10, C = 9, K = 2;        solve(A, B, C, K);    } } // This code is contributed by AnkThon
# Python3 program for the above approach # Function to find the difference between# closest multiple of A, B, C with Kdef solve( A, B, C, K) :     # Stores the multiples of A, B, C    # just greater than K     # Multiple of A    if ((K % A) != 0) :        fa = ((K // A) + 1) * A;    else :        fa = (K // A) * A;     # Multiple of B    if ((K % B) != 0) :         fb = ((K // B) + 1) * B;         else :        fb = (K // B) * B;     # Multiple of C    if ((K % C) != 0) :         fc = ((K // C) + 1) * C;    else :        fc = (K // C) * C;     # Store the resultant answer    ans = min(fa - K, min(fc - K, fb - K));         print(ans); # Drive codeif __name__ == "__main__" :     A = 6; B = 10; C = 9; K = 2;    solve(A, B, C, K);     # This code is contributed by AnkThon
// C# program for the above approachusing System;public class GFG {     // Function to find the difference between    // closest multiple of A, B, C with K    static void solve(int A, int B, int C, int K)    {         // Stores the multiples of A, B, C        // just greater than K        int fa, fb, fc;         // Multiple of A        if ((K % A) != 0) {             fa = ((K / A) + 1) * A;        }        else {            fa = (K / A) * A;        }         // Multiple of B        if ((K % B) != 0) {             fb = ((K / B) + 1) * B;        }        else {            fb = (K / B) * B;        }         // Multiple of C        if ((K % C) != 0) {             fc = ((K / C) + 1) * C;        }        else {            fc = (K / C) * C;        }         // Store the resultant answer        int ans;        ans = Math.Min(fa - K, Math.Min(fc - K, fb - K));        Console.WriteLine(ans);    }     // Drive code    public static void Main(string[] args)    {        int A = 6, B = 10, C = 9, K = 2;        solve(A, B, C, K);    }} // This code is contributed by ukasp.
<script>// javascript program for the above approach        // Function to find the difference between    // closest multiple of A, B, C with K    function solve(A , B , C , K)    {               // Stores the multiples of A, B, C        // just greater than K        var fa, fb, fc;             // Multiple of A        if ((K % A) != 0) {                 fa = parseInt((K / A) + 1) * A;        }        else {            fa = parseInt(K / A) * A;        }             // Multiple of B        if ((K % B) != 0) {                 fb = parseInt((K / B) + 1) * B;        }        else {            fb = parseInt(K / B) * B;        }             // Multiple of C        if ((K % C) != 0) {                 fc = parseInt((K / C) + 1) * C;        }        else {            fc = parseInt(K / C) * C;        }             // Store the resultant answer        var ans;        ans = Math.min(fa - K, Math.min(fc - K, fb - K));        document.write(ans);    } // Drive codevar A = 6, B = 10, C = 9, K = 2;solve(A, B, C, K); // This code is contributed by 29AjayKumar</script>
  
4
 
Time Complexity: O(1)Auxiliary Space: O(1)
 
lokeshpotta20
shikhasingrajput
_saurabh_jaiswal
ankthon
sanjoy_62
ukasp
29AjayKumar
arorakashish0911
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Median in a stream of integers (running integers)
Program to multiply two matrices
Generate all permutation of a set in Python
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Newton's Divided Difference Interpolation Formula
Program to print prime numbers from 1 to N.
Singular Value Decomposition (SVD)
Fizz Buzz Implementation
Binomial Coefficient | DP-9
Program for Muller Method