Sunday, November 17, 2024
Google search engine
HomeData Modelling & AICount numbers present in partitions of N

Count numbers present in partitions of N

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 = 

\binom{n-1}{k-1}
 

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

 

    \begin{align*} = \sum_{k=1}^nk\binom{n-1}{k-1}&=\sum_{k=0}^{n-1}(k+1)\binom{n-1}k\\ &=(n-1)\sum_{k=0}^{n-1}\binom{n-2}{k-1}+\sum_{k=0}^{n-1}\binom{n-1}k\\ &=(n-1)2^{n-2}+2^{n-1}\\ &=(n+1)2^{n-2}\, . \end{align*}

 

Below is the implementation of the above approach:

 

C++




// C++ program to implement
// the above approach 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count of numbers in
// ordered partitions of N
int CtOfNums(int N)
{
  
    // Stores count the numbers in
    // ordered integer partitions
    int res = (N + 1) * (1 << (N - 2));
  
    return round(res);
}
  
// Driver Code
int 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 N
static int CtOfNums(int N)
{
  
    // Stores count the numbers in
    // ordered integer partitions
    int res = (N + 1) * (1 << (N - 2));
  
    return Math.round(res);
}
  
// Driver Code
public 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 N
def CtOfNums(N):
 
    # Stores count the numbers in
    # ordered integer partitions
    res = (N + 1) * (1<<(N - 2))
 
    return round(res)
 
# Driver code
if __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 N
static 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 Code
public static void Main ()
{
    int N = 3;
  
    Console.Write(CtOfNums(N));
}
}
 
// This code is contributed by code_hunt


Javascript




<script>
 
// Javascript program to implement
// the above approach 
 
// Function to count of numbers in
// ordered partitions of N
function CtOfNums(N)
{
  
    // Stores count the numbers in
    // ordered integer partitions
    var res = (N + 1) * (1 << (N - 2));
  
    return Math.round(res);
}
  
// Driver Code
var N = 3;
document.write(CtOfNums(N));
 
</script>


Output: 

8

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments