Count of pairs in range [ P , Q ] with numbers as multiple of R and their product lie in range [ P * Q / 4 , P * Q ] | Java program for above approach ; Function to find the number of pairs such that both the elements are in the range [ P , Q ] and the numbers should be multiple of R , and the product of numbers should lie in the range [ P * Q / 4 , P * Q ] ; Store multiple of r in range of [ P , Q ] ; Itearte in the range [ p , q ] ; Vector to store pair of answer ; Iterate through the vector v ; Iterate in the range [ i + 1 , v . size ( ) - 1 ] ; If pair follow this condition insert the pair in vector ans ; If no pair satisfy the conditions , print - 1 ; Print the pairs which satisfy the given condition ; Driver Code ; Given Input ; Function Call
import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void findPairs ( int p , int q , int r ) { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = p ; i <= q ; i ++ ) { if ( i % r == 0 ) { v . add ( i ) ; } } ArrayList < pair < Integer , Integer > > ans = new ArrayList < > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { if ( v . get ( i ) * v . get ( j ) >= p * q / 4 && v . get ( i ) * v . get ( j ) <= p * q ) { pair < Integer , Integer > x = new pair < > ( ) ; x . first = v . get ( i ) ; x . second = v . get ( j ) ; ans . add ( x ) ; } } } if ( ans . size ( ) == 0 ) { System . out . println ( - 1 ) ; } else { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . println ( ans . get ( i ) . first + " ▁ " + ans . get ( i ) . second ) ; } } } public static void main ( String [ ] args ) { int p = 14 , q = 30 , r = 5 ; findPairs ( p , q , r ) ; } }