Count ways to remove objects such that exactly M equidistant objects remain | C ++ program for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { cout << n ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } cout << ans ; } int main ( ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; return 0 ; }