Given a sum S and product P, the task is to print any pair of integers which has the sum S and the product P. If there exists no such pair, then print -1.
Input: S = 5, P = 6
Output: 2, 3
Sum = 2 + 3 = 5, and
Product = 2 * 3 = 6
Input: S = 5, P = 9
No such pair exists
Approach: Let the pair be (x, y). Therefore, according to the problem, the given sum (S) will be (x + y) and the given product (P) will be (x * y)
If the pair is (x, y) Given that product P = x * y y = P / x; (eq.. 1) Given that sum S = x + y S = x + (P / x) from (eq..1) x2 - Sx + P = 0 which is a quadratic equation in x.
Since this is a quadratic equation, we just need to find it’s roots, using the below equation.
Here: a = 1 b = -S c = P
Therefore the above equation will be changed as:
Below is the implementation of the above approach:
3, 2 -1
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.
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Print N numbers such that their product is a Perfect Cube
- Construct an array from its pair-product
- Maximum possible GCD for a pair of integers with product N
- Pair with min absolute difference and whose product is N+1 or N+2
- Find a pair in Array with second largest product
- Count of elements which is product of a pair or an element square
- Find a pair with maximum product in array of Integers
- Check if a given pair of Numbers are Betrothed numbers or not
- Print all possible pair with prime XOR in the Array
- Print any pair of integers with sum of GCD and LCM equals to N
- Numbers less than N which are product of exactly two distinct prime numbers
- Check if the given two numbers are friendly pair or not
- Program to print product of even and odd indexed elements in an Array
- Pair of prime numbers with a given sum and minimum absolute difference
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Product of 2 Numbers using Recursion
- Find two numbers with sum and product both same as N
- Check whether product of 'n' numbers is even or odd
- First digit in product of an array of numbers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.