Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Java implementation of the above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; if first element is bigger then swap ; count is store the number of pair . ; we use temp for breaking a loop . ; count when a is greater . ; Count when a is smaller but b is greater ; Count when a and b both are smaller ; breaking condition ; For storing The pair in count . ; return the number of pairs . ; Driver code
class GFG { public static int findCountOfPairs ( int a , int b , int n ) { if ( a > b ) { int temp = a ; a = b ; b = temp ; } int temp = 1 , count = 0 ; for ( int i = n ; temp > 0 ; i += n ) { if ( a >= i ) { temp = i - 1 ; } else if ( b >= i ) { temp = a ; } else if ( i > b ) { temp = a - ( i - b ) + 1 ; } if ( temp > 0 ) { count += temp ; } } return count ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . print ( findCountOfPairs ( a , b , n ) ) ; } }