Given the system of equations a2 + b = n and a + b2 = m. The task is to find the number of pair of positive integers (a, b) which satisfy the equation for given n and m.
Input: n = 9, m = 3
Only one pair (3, 0) exists for both equations satisfying the conditions.
Input: n = 4, m = 20
There are no such pair exists.
The approach is to check for all possible pairs of numbers and check if that pair satisfy both the equations or not. For this we have,
a2 + b = n ... (1) a + b2 = m ... (2) For equation (2), => a = m - b2 ... (3)
- Now for the positive value of a, every value of b must be from 0 to sqrt(m).
- Obtain the value of a from equations (3).
- If the pair (a, b) satisfy equation (1), then pair (a, b) is the solution of system of equations.
Below is the implementation of the above approach:
Time Complexity: O(sqrt(min(n,m))
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.