Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | Java program for the above approach ; Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to Math . sqrt ( N ) ; Maximum possible value of p is Math . min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
import java . util . * ; class GFG { static int countPairs ( int N ) { int res = 0 ; for ( int q = 1 ; q * q <= N ; q ++ ) { int maxP = Math . min ( 2 * N - q , N / q ) ; if ( maxP < q ) continue ; int cnt = maxP - q + 1 ; res += ( cnt / 2 + ( cnt & 1 ) ) ; } return res ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( countPairs ( N ) ) ; } }