The Wayback Machine - https://web.archive.org/web/20211130120041/https://www.geeksforgeeks.org/minimize-difference-between-any-multiple-of-given-three-numbers-with-k/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize difference between any multiple of given three numbers with K

  • Last Updated : 30 Nov, 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:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

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++




// 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 K
void 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 mutlitplication 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 Code
int main()
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
    return 0;
}

Java




// Java program for the above approach
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 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 mutlitplication 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 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 shikhasingrajput

Python3




# Python Program to implement
# the above approach
 
# Function to find the difference between
# closest multiple of A, B, C with K
def 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 mutlitplication 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 Code
A = 6
B = 10
C = 9
K = 2
solve(A, B, C, K)
 
 # This code is contributed by saurabh_jaiswal.

Javascript




<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 mutlitplication 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>

 
 

Output
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++




// 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 K
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;
    }
 
    // Mulitple 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 code
int main()
{
    int A = 6, B = 10, C = 9, K = 2;
    solve(A, B, C, K);
    return 0;
}

Python3




# Python3 program for the above approach
 
# Function to find the difference between
# closest multiple of A, B, C with K
def 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;
 
    # Mulitple 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 code
if __name__ == "__main__" :
 
    A = 6; B = 10; C = 9; K = 2;
    solve(A, B, C, K);
 
    # This code is contributed by AnkThon

 
 

Output
4

 

Time Complexity: O(1)
Auxiliary Space: O(1)

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :