# Pair of integers (a, b) which satisfy the given equations

Given the system of equations **a ^{2} + b = n** and

**a + b**. The task is to find the number of pair of positive integers

^{2}= m**(a, b)**which satisfy the equation for given

**n**and

**m**.

**Examples:**

Input:n = 9, m = 3

Output:1

Explanation:

Only one pair (3, 0) exists for both equations satisfying the conditions.

Input:n = 4, m = 20

Output:0

Explanation:

There are no such pair exists.

**Approach:**

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,

a^{2}+ b = n ... (1) a + b^{2}= m ... (2) For equation (2), => a = m - b^{2}... (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:

## C++

`// C++ program to count the pair of integers(a, b) ` `// which satisfy the equation ` `// a^2 + b = n and a + b^2 = m ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to count valid pairs ` `int` `pairCount(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cnt = 0, b, a; ` ` ` `for` `(b = 0; b <= ` `sqrt` `(m); b++) { ` ` ` `a = m - b * b; ` ` ` `if` `(a * a + b == n) { ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 9, m = 3; ` ` ` ` ` `cout << pairCount(n, m) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the pair of integers(a, b) ` `// which satisfy the equation ` `// a^2 + b = n and a + b^2 = m ` `class` `GFG ` `{ ` ` ` `// Function to count valid pairs ` `static` `int` `pairCount(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cnt = ` `0` `, b, a; ` ` ` `for` `(b = ` `0` `; b <= Math.sqrt(m); b++) ` ` ` `{ ` ` ` `a = m - b * b; ` ` ` `if` `(a * a + b == n) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `9` `, m = ` `3` `; ` ` ` `System.out.print(pairCount(n, m) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count the pair of integers(a, b) ` `# which satisfy the equation ` `# a^2 + b = n and a + b^2 = m ` ` ` `# Function to count valid pairs ` `def` `pairCount(n, m): ` ` ` `cnt ` `=` `0` `; ` ` ` `for` `b ` `in` `range` `(` `int` `(` `pow` `(m, ` `1` `/` `2` `))): ` ` ` `a ` `=` `m ` `-` `b ` `*` `b; ` ` ` `if` `(a ` `*` `a ` `+` `b ` `=` `=` `n): ` ` ` `cnt ` `+` `=` `1` `; ` ` ` ` ` `return` `cnt; ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `n ` `=` `9` `; ` ` ` `m ` `=` `3` `; ` ` ` `print` `(pairCount(n, m)); ` ` ` `# This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the pair of integers(a, b) ` `// which satisfy the equation ` `// a^2 + b = n and a + b^2 = m ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to count valid pairs ` `static` `int` `pairCount(` `int` `n, ` `int` `m) ` `{ ` ` ` `int` `cnt = 0, b, a; ` ` ` `for` `(b = 0; b <= Math.Sqrt(m); b++) ` ` ` `{ ` ` ` `a = m - b * b; ` ` ` `if` `(a * a + b == n) ` ` ` `{ ` ` ` `cnt++; ` ` ` `} ` ` ` `} ` ` ` `return` `cnt; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 9, m = 3; ` ` ` `Console.Write(pairCount(n, m) +` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(sqrt(min(n,m))

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Median in a stream of integers (running integers)
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n
- Find a pair with maximum product in array of Integers
- Gaussian Elimination to Solve Linear Equations
- Count 'd' digit positive integers with 0 as a digit
- Count positive integers with 0 as a digit and maximum 'd' digits
- Linear Diophantine Equations
- How to sum two integers without using arithmetic operators in C/C++?
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Number of sextuplets (or six values) that satisfy an equation
- Number of substrings divisible by 6 in a string of integers
- Multiply large integers under large modulo
- Using Chinese Remainder Theorem to Combine Modular equations
- Check if a number can be written as sum of three consecutive integers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.