Count of pairs from first N natural numbers with remainder at least K | Function to count the number of pairs ( a , b ) such that a % b is at least K ; Base Case ; Stores resultant count of pairs ; Iterate over the range [ K + 1 , N ] ; Find the cycled elements ; Find the remaining elements ; Return the resultant possible count of pairs ; Driver Code
def countTotalPairs ( N , K ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return N * N NEW_LINE DEDENT ans = 0 NEW_LINE for b in range ( K + 1 , N + 1 ) : NEW_LINE INDENT ans += ( N // b ) * ( b - K ) NEW_LINE ans += max ( N % b - K + 1 , 0 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 5 NEW_LINE K = 2 NEW_LINE print ( countTotalPairs ( N , K ) ) NEW_LINE