Count ways to remove objects such that exactly M equidistant objects remain | Function to count the number of ways of removing objects such that after removal , exactly M equidistant objects remain ; Store the resultant number of arrangements ; Base Case : When only 1 object is left ; Print the result and return ; Iterate until len <= n and increment the distance in each iteration ; Total length if adjacent objects are d distance apart ; If len > n ; Update the number of ways ; Print the result ; Driver Code
function waysToRemove ( n , m ) { var ans = 0 ; if ( m == 1 ) { document . write ( n ) ; return ; } for ( var d = 0 ; d >= 0 ; d ++ ) { var len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } document . write ( ans ) ; } var N = 5 , M = 3 ; waysToRemove ( N , M ) ;