Count ways to remove objects such that exactly M equidistant objects remain
Last Updated :
27 Apr, 2021
Given an integer N, representing objects placed adjacent to each other, the task is to count the number of ways to remove objects such that after their removal, exactly M objects are left and the distance between each adjacent object is equal.
Examples:
Input: N = 5, M = 3
Output: 4
Explanation:
Let the initial arrangement be A1 A2 A3 A4 A5.
The following arrangements are possible:
- A1 A2 A3 _ _
- _ A2 A3 A4 _
- _ _ A3 A4 A5
- A1_ A3_ A5
Therefore, the total count of possible arrangements is 4.
Input: N = 2, M = 1
Output: 2
Approach: The idea is based on the observation that an arrangement of M objects with D adjacent spaces takes (M + (M – 1) * D) length, say L. For this arrangement, there are (N – L + 1) options. Therefore, the idea is to iterate over D from 0 till L ? N and find the number of ways accordingly.
Follow the steps below to solve the problem:
- If the value of M is 1, then the number of possible arrangements is N. Therefore, print the value of N.
- Otherwise, perform the following steps:
- Initialize two variables, say ans to 0, to store the total number of required arrangements.
- Iterate a loop using a variable D. Perform the following steps:
- Store the total length required for the current value of D in a variable, say L as M + (M – 1) * D.
- If the value of L is greater than N, then break out of the loop.
- Otherwise, update the number of arrangements by adding the value (N – L + 1) to the variable ans.
- After completing the above steps, print the value of ans as the total number of arrangements.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
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;
}
|
Java
import java.io.*;
class GFG{
static void waysToRemove( int n, int m)
{
int ans = 0 ;
if (m == 1 )
{
System.out.println(n);
return ;
}
for ( int d = 0 ; d >= 0 ; d++)
{
int len = m + (m - 1 ) * d;
if (len > n)
break ;
ans += (n - len) + 1 ;
}
System.out.println(ans);
}
public static void main(String[] args)
{
int N = 5 , M = 3 ;
waysToRemove(N, M);
}
}
|
Python3
def waysToRemove(n, m):
ans = 0
if (m = = 1 ):
print (n)
return
d = 0
while d > = 0 :
length = m + (m - 1 ) * d
if (length > n):
break
ans + = (n - length) + 1
d + = 1
print (ans)
if __name__ = = "__main__" :
N = 5
M = 3
waysToRemove(N, M)
|
C#
using System;
class GFG
{
static void waysToRemove( int n, int m)
{
int ans = 0;
if (m == 1)
{
Console.Write(n);
return ;
}
for ( int d = 0; d >= 0; d++)
{
int len = m + (m - 1) * d;
if (len > n)
break ;
ans += (n - len) + 1;
}
Console.Write(ans);
}
static void Main()
{
int N = 5, M = 3;
waysToRemove(N, M);
}
}
|
Javascript
<script>
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);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...