Count possible values of K such that X | C ++ program for the above approach ; Function to count integers K satisfying given equation ; Calculate the absoluter difference between a and b ; Iterate till sqrt of the difference ; Return the count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int condition ( int a , int b ) { int d = abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } int main ( ) { int x = 2 , y = 6 ; cout << condition ( x , y ) << endl ; return 0 ; }