Count of pairs from first N natural numbers with remainder at least K | package whatever do not write package name here ; 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 element ; Find the remaining element ; Return the resultant possible count of pairs ; Driver code
import java . io . * ; class GFG { public static int countTotalPairs ( int N , int K ) { if ( K == 0 ) { return N * N ; } int ans = 0 ; for ( int i = K + 1 ; i <= N ; i ++ ) { ans += ( N / i ) * ( i - K ) ; if ( ( N % i ) - K + 1 > 0 ) { ans += ( N % i ) - K + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int N = 5 , K = 2 ; System . out . println ( countTotalPairs ( N , K ) ) ; } }