Related Articles

# Count numbers present in partitions of N

• Last Updated : 17 Mar, 2021

Given an integer N, the task is to count the numbers in ordered integer partitions of N.
Examples:

Input: N = 3
Output:
Integer partitions of N(=3) are {{1 + 1 + 1}, {1 + 2}, {2 + 1}, {3}}.
Numbers in integer partition of N are:{1, 1, 1, 1, 2, 2, 1, 3}
Therefore, the count of numbers in integer partitions of N(=3) is 8.

Input: N = 4
Output: 20

Approach: The problem can be solved based on the following observations:

Count of ways to partition N into exactly k partitions =

Therefore, the count of numbers in ordered integer partitions of N is

Below is the implementation of the above approach:

## C++

 // C++ program to implement// the above approach #include using namespace std; // Function to count of numbers in// ordered partitions of Nint CtOfNums(int N){      // Stores count the numbers in    // ordered integer partitions    int res = (N + 1) * (1 << (N - 2));      return round(res);}  // Driver Codeint main(){    int N = 3;         cout << CtOfNums(N);} // This code is contributed by code_hunt

## Java

 // Java program to implement// the above approach import java.io.*; class GFG{  // Function to count of numbers in// ordered partitions of Nstatic int CtOfNums(int N){      // Stores count the numbers in    // ordered integer partitions    int res = (N + 1) * (1 << (N - 2));      return Math.round(res);}  // Driver Codepublic static void main (String[] args){    int N = 3;      System.out.print(CtOfNums(N));}} // This code is contributed by code_hunt

## Python3

 # Python3 program to implement# the above approach # Function to count of numbers in# ordered partitions of Ndef CtOfNums(N):     # Stores count the numbers in    # ordered integer partitions    res = (N + 1) * (1<<(N - 2))     return round(res) # Driver codeif __name__ == '__main__':    N = 3    print(CtOfNums(N))

## C#

 // C# program to implement// the above approach using System; class GFG{  // Function to count of numbers in// ordered partitions of Nstatic int CtOfNums(int N){      // Stores count the numbers in    // ordered integer partitions    double res = (N + 1) * (1 << (N - 2));      return (int)Math.Round(res);}  // Driver Codepublic static void Main (){    int N = 3;      Console.Write(CtOfNums(N));}} // This code is contributed by code_hunt

## Javascript

 
Output:
8

Time Complexity: O(log2N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up