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.
Examples:
Input: N = 2Â
Output: 1
Explanation: There can be only one group.Input: N = 10
Output: 3
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:
C++
// C++ program for the given approach#include <iostream>using namespace std;Â
// Function to find the number of waysint numberofWays(int N){    // Variable to store the number of ways    int ways = 0;    int i;Â
    // Loop to find total number of ways    for (i = 1; i <= N / 2; i++) {        if (N % i == 0)            ways++;    }Â
    // Returning the number of ways    return ways;}Â
// Driver Codeint main(){Â
    // Declaring and initialising N    int N = 10;Â
    // Function call    int ans = numberofWays(N);Â
    // Displaying the answer on screen    cout << ans;    return 0;} |
Java
// Java program for the above approachimport java.io.*;import java.lang.*;import java.util.*;Â
class GFG {Â
  // Function to find the number of ways  static int numberofWays(int N)  {    // Variable to store the number of ways    int ways = 0;    int i;Â
    // Loop to find total number of ways    for (i = 1; i <= N / 2; i++) {      if (N % i == 0)        ways++;    }Â
    // Returning the number of ways    return ways;  }Â
  public static void main (String[] args)  {Â
    // Declaring and initialising N    int N = 10;Â
    // Function call    int ans = numberofWays(N);Â
    // Displaying the answer on screen    System.out.print(ans);  }}Â
// This code is contributed by hrithikgarg03188 |
Python3
# Python code for the above approach Â
# Function to find the number of waysdef numberofWays(N):Â
    # Variable to store the number of ways    ways = 0;    i = NoneÂ
    # Loop to find total number of ways    for i in range(1, (N // 2) + 1):        if (N % i == 0):            ways += 1Â
    # Returning the number of ways    return ways;Â
# Driver CodeÂ
# Declaring and initialising NN = 10;Â
# Function callans = numberofWays(N);Â
# Displaying the answer on screenprint(ans);Â
# This code is contributed by Saurabh Jaiswal |
C#
// C# program for the above approachusing System;Â
class GFG {Â
  // Function to find the number of ways  static int numberofWays(int N)  {    // Variable to store the number of ways    int ways = 0;    int i;Â
    // Loop to find total number of ways    for (i = 1; i <= N / 2; i++) {      if (N % i == 0)        ways++;    }Â
    // Returning the number of ways    return ways;  }Â
  public static void Main(string[] args)  {Â
    // Declaring and initialising N    int N = 10;Â
    // Function call    int ans = numberofWays(N);Â
    // Displaying the answer on screen    Console.WriteLine(ans);  }}Â
// This code is contributed by ukasp. |
Javascript
<script>Â Â Â Â Â Â Â Â // JavaScript code for the above approach Â
        // Function to find the number of ways        function numberofWays(N)        {                     // Variable to store the number of ways            let ways = 0;            let i;Â
            // Loop to find total number of ways            for (i = 1; i <= Math.floor(N / 2); i++) {                if (N % i == 0)                    ways++;            }Â
            // Returning the number of ways            return ways;        }Â
        // Driver CodeÂ
        // Declaring and initialising N        let N = 10;Â
        // Function call        let ans = numberofWays(N);Â
        // Displaying the answer on screen        document.write(ans);Â
  // This code is contributed by Potta Lokesh    </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!


