
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +
y*y < n in C++
We are given a positive integer N. The goal is to count the pairs of distinct non-negative positive integers that satisfy the inequality: x*x + y*y < N
We will start from x=0 to x2 < N and y=0 to y2 < N . If any x2 + y2 < N, increase count of pairs.
Input
n=4
Output
distinct pairs= 4
Explanation − pairs will be (0,0), (1,1), (0,1), (1,0). All these satisfy the inequality x2 + y2 < 4
Input
n=2
Output
distinct pairs= 3
Explanation − pairs will be (0,0), (0,1), (1,0). All these satisfy the inequality x2 + y2 < 2
Approach used in the below program is as follows
The integer N stores the positive integer.
Function countPairs(int n) takes n as input and returns the count of distinct non-negative positive integer pairs that satisfy the inequality:x2 + y2 < n.
count stores the number of such pairs , initially 0.
Start from i=0 to i2 < n and other loop j=0 to j2 < n.
If i2 + j2 < n increment count.
Return count in the end as result.
Example
#include <iostream> using namespace std; int countPairs(int n){ int count = 0; for (int i = 0; i*i < n; i++) for (int j = 0; j*j < n; j++) //x*x + y*y < n if(i*i + j*j < n) count++; return count; } int main(){ int N=4; cout << "Distinct Non-Negative integer pairs count: " << countPairs(N) ; return 0; }
Output
Distinct Non-Negative integer pairs count: 4
- Related Articles
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count of pairs (x, y) in an array such that x < y in C++
- Find x, y, z that satisfy 2/n = 1/x + 1/y + 1/z in C++
- Find number of pairs (x, y) in an array such that x^y > y^x in C++
- Find a distinct pair (x, y) in given range such that x divides y in C++
- Solve the following pairs of equations:\( \frac{2 x y}{x+y}=\frac{3}{2} \)\( \frac{x y}{2 x-y}=\frac{-3}{10}, x+y ≠ 0,2 x-y ≠ 0 \)
- Find the sum of the following arithmetic progressions:\( \frac{x-y}{x+y}, \frac{3 x-2 y}{x+y}, \frac{5 x-3 y}{x+y}, \ldots \) to \( n \) terms
- Factorize the algebraic expression $a(x-y)+2b(y-x)+c(x-y)^2$.
- Solve the following pairs of equations:\( x+y=3.3 \)\( \frac{0.6}{3 x-2 y}=-1,3 x-2 y ≠ 0 \)
- Verify : (i) \( x^{3}+y^{3}=(x+y)\left(x^{2}-x y+y^{2}\right) \)(ii) \( x^{3}-y^{3}=(x-y)\left(x^{2}+x y+y^{2}\right) \)
- If \( a=x^{m+n} y^{l}, b=x^{n+l} y^{m} \) and \( c=x^{l+m} y^{n} \), prove that \( a^{m-n} b^{n-1} c^{l-m}=1 . \)
- Factorize:\( x^{2}+y-x y-x \)
- Factorize:$4(x - y)^2 - 12(x -y) (x + y) + 9(x + y)^2$
- If \( 3^{x}=5^{y}=(75)^{z} \), show that \( z=\frac{x y}{2 x+y} \).
- Factorize the algebraic expression $a^2(x+y)+b^2(x+y)+c^2(x+y)$.
