Number of ways to divide a N elements equally into group of at least 2
Given an integer N denoting the number of elements, the task is to find the number of ways to divide these elements equally into groups such that each group has at least 2 elements.
Input: N = 2
Explanation: There can be only one group.
Input: N = 10
Explanation: There are 3 ways to divide elements:
One group having all the 10 elements.
Two groups where each group has 5 elements.
Five groups where each group has 2 elements.
Approach: The above problem can be solved using the below-given brute force approach. In every iteration of the loop, i represents the number of groups. If N is completely divisible by i, hence, the elements can be equally divided among groups. Follow the steps below to solve the problem:
- Declare variable ways and initialize it by 0.
- Iterate over the range [1, N/2] using the variable i and perform the following tasks:
- Check if N is completely divisible by i.
- If yes, then increment ways by 1.
- After performing the above steps, print the value of ways as the answer.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(1)
Please Login to comment...