Given a, b and n. Find x and y that satisfies ax + by = n. Print any of the x and y satisfying the equation
Input : n=7 a=2 b=3 Output : x=2, y=1 Explanation: here x and y satisfies the equation Input : 4 2 7 Output : No solution
We can check if any solutions exists or not using Linear Diophantine Equations, but here we need to find out the solutions for this equation, so we can simply iterate for all possible values from 0 to n as it cannot exceed n for this given equation. So solving this equation with pen and paper gives y=(n-ax)/b and similarly we get the other number to be x=(n-by)/a.If none of the values satisfies the equation, at the end we print “no solution”
x = 2, y = 1
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- DBMS | Minimum relations satisfying 1NF
- Number of ordered points pair satisfying line equation
- Union-Find Algorithm | (Union By Rank and Find by Optimized Path Compression)
- Find value of (1^n + 2^n + 3^n + 4^n ) mod 5
- std::find in C++
- Find value of (n^1 + n^2 + n^3 + n^4) mod 5 for given n
- Find the value of max(f(x)) - min(f(x)) for a given F(x)
- Program to find sum of 1 + x/2! + x^2/3! +...+x^n/(n+1)!
- Find next right node of a given key | Set 2
- Find maximum among x^(y^2) or y^(x^2) where x and y are given
- Find larger of x^y and y^x
- Find if there is a subarray with 0 sum
- Find a pair with given sum in BST
- Find the sum of the series x(x+y) + x^2(x^2+y^2) +x^3(x^3+y^3)+ ... + x^n(x^n+y^n)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m