
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x + y*y < n in C++
- Count Distinct Non-Negative Integer Pairs (x, y) that Satisfy the Inequality x*x +\ny*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++
- Find larger of x^y and y^x in C++
- Find maximum among x^(y^2) or y^(x^2) where x and y are given in C++
- Check if a number can be expressed as x^y (x raised to power y) in C++
- Find x and y satisfying ax + by = n in C++
- What is ternary operator (? X : Y) in C++?
- Find the smallest number X such that X! contains at least Y trailing zeros in C++
- Find the Number of solutions for the equation x + y + z <= n using C++
- Write an iterative O(Log y) function for pow(x, y) in C++
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array in C++
- Maximize the value of x + y + z such that ax + by + cz = n in C++