Count of pairs from 1 to a and 1 to b whose sum is divisible by N | 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
function findCountOfPairs ( a , b , n ) { if ( a > b ) { let temp1 = a ; a = b ; b = temp1 ; } let temp = 1 , count = 0 ; for ( let 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 ; } let a = 5 , b = 13 , n = 3 ; document . write ( findCountOfPairs ( a , b , n ) ) ;