Open In App

Minimize difference between any multiple of given three numbers with K

Last Updated : 24 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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




// 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 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 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 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 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 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 Code
A = 6
B = 10
C = 9
K = 2
solve(A, B, C, K)
 
 # This code is contributed by saurabh_jaiswal.


C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
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 = 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.


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


 
 

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;
    }
 
    // 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 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
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));
        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




# 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;
 
    # 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 code
if __name__ == "__main__" :
 
    A = 6; B = 10; C = 9; K = 2;
    solve(A, B, C, K);
 
    # This code is contributed by AnkThon


C#




// C# program for the above approach
using 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.


Javascript




<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 code
var A = 6, B = 10, C = 9, K = 2;
solve(A, B, C, K);
 
// This code is contributed by 29AjayKumar
</script>


 
 

Output

4

 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads