Thursday, January 16, 2025
Google search engine
HomeData Modelling & AIMinimum positive integer value possible of X for given A and B...

Minimum positive integer value possible of X for given A and B in X = P*A + Q*B

Given values of A and B, find the minimum positive integer value of X that can be achieved in the equation X = P*A + P*B, Here P and Q can be zero or any positive or negative integer. 
Examples: 

Input: A = 3
        B = 2
Output: 1

Input: A = 2
         B = 4 
Output: 2

Basically we need to find P and Q such that P*A > P*B and P*A – P*B is minimum positive integer. This problem can be easily solved by calculating GCD of both numbers. 
For example: 
 

For A = 2 
And B = 4
Let P  = 1
And Q = 0            
X = P*A + Q*B
  = 1*2 + 0*4
  = 2 + 0
  = 2 (i. e GCD of 2 and 4)

For A = 3
and B = 2
let P = -1
And Q = 2
X = P*A + Q*B
  = -1*3 + 2*2
  = -3 + 4
  = 1 ( i.e GCD of 2 and 3 )

Below is the implementation of above idea:

CPP




// CPP Program to find
// minimum value of X
// in equation X = P*A + Q*B
 
#include <bits/stdc++.h>
using namespace std;
 
// Utility function to calculate GCD
int gcd(int a, int b)
{
    if (a == 0)
        return b;
    return gcd(b % a, a);
}
 
// Driver Code
int main()
{
    int a = 2;
    int b = 4;
    cout << gcd(a, b);
    return 0;
}


Java




// Java Program to find
// minimum value of X
// in equation X = P*A + Q*B
import java.util.*;
import java.lang.*;
 
class GFG {
    // utility function to calculate gcd
 
    public static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
 
        return gcd(b % a, a);
    }
 
    // Driver Program
    public static void main(String[] args)
    {
        int a = 2;
        int b = 4;
 
        System.out.println(gcd(a, b));
    }
}


Python3




# Python3 Program to find
# minimum value of X
# in equation X = P * A + Q * B
 
# Function to return gcd of a and b
 
 
def gcd(a, b):
    if a == 0:
        return b
    return gcd(b % a, a)
 
 
a = 2
b = 4
print(gcd(a, b))


C#




// CSHARP Program to find
// minimum value of X
// in equation X = P*A + Q*B
using System;
 
class GFG {
    // function to get gcd of a and b
    public static int gcd(int a, int b)
    {
        if (a == 0)
            return b;
 
        return gcd(b % a, a);
    }
 
    // Driver Code
    static public void Main()
    {
        int a = 2;
        int b = 4;
        Console.WriteLine(gcd(a, b));
    }
}


PHP




// PHP Program to find
// minimum value of X
// in equation X = P*A + Q*B
 
<?php
 
// Function to return
// gcd of a and b
function gcd($a, $b)
{
    if ($a == 0)
        return $b;
    return gcd($b % $a, $a);
}
  
// Driver Code
$a = 2;
$b = 4;
echo gcd($a, $b);
 
?>


Javascript




<script>
// javascript Program to find
// minimum value of X
// in equation X = P*A + Q*B
 
    // utility function to calculate gcd
 
    function gcd(a , b) {
        if (a == 0)
            return b;
 
        return gcd(b % a, a);
    }
 
    // Driver Program
     
        var a = 2;
        var b = 4;
 
        document.write(gcd(a, b));
 
// This code contributed by umadevi9616
</script>


 
 

Output

2

Time Complexity: O(log(min(a, b)))

Auxiliary Space: O(log(min(a, b)))
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments