Given a number N, the task is to count all ‘a’ and ‘b’ that satisfy the condition a^2 + b^2 = N.
Note:- (a, b) and (b, a) are to be considered as two different pairs and (a, a) is also valid and to be considered only one time.
Input: N = 10 Output: 2 1^2 + 3^2 = 9 3^2 + 1^2 = 9 Input: N = 8 Output: 1 2^2 + 2^2 = 8
- Traverse numbers from 1 to square root of N.
- Subtract square of the current number from N and check if their difference is a perfect square or not.
- If it is perfect square then increment the count.
- Return count.
Below is the implementation of above approach:
For n = 1, 1 pair exists For n = 2, 1 pair exists For n = 3, 0 pair exists For n = 4, 1 pair exists For n = 5, 2 pair exists For n = 6, 0 pair exists For n = 7, 0 pair exists For n = 8, 1 pair exists For n = 9, 1 pair exists For n = 10, 2 pair exists
Time Complexity : O(sqrt(N))
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.
- Count of pairs of integers whose difference of squares is equal to N
- Count all subarrays whose sum can be split as difference of squares of two Integers
- Minimum number of squares whose sum equals to given number N | set 2
- Minimum number of squares whose sum equals to a given number N | Set-3
- Minimum number of squares whose sum equals to given number n
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Count pairs (a, b) whose sum of cubes is N (a^3 + b^3 = N)
- Count of pairs in an array whose sum is a perfect square
- Count pairs in array whose sum is divisible by K
- Given two arrays count all pairs whose sum is an odd number
- Count of pairs in an Array whose sum is a Perfect Cube
- Count of pairs in an Array whose sum is Prime
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count pairs whose sum consists of set bits only
- Count pairs from a given range whose sum is a Prime Number in that range
- Count of pairs from 1 to a and 1 to b whose sum is divisible by N
- Largest sub-array whose all elements are perfect squares
- Generate Linked List consisting of maximum difference of squares of pairs of nodes from given Linked List
- Sum of the count of number of adjacent squares in an M X N grid
- Count of primes in a given range that can be expressed as sum of perfect squares
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.