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: 8
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++
#include <bits/stdc++.h>
using namespace std;
int CtOfNums( int N)
{
int res = (N + 1) * (1 << (N - 2));
return round(res);
}
int main()
{
int N = 3;
cout << CtOfNums(N);
}
|
Java
import java.io.*;
class GFG{
static int CtOfNums( int N)
{
int res = (N + 1 ) * ( 1 << (N - 2 ));
return Math.round(res);
}
public static void main (String[] args)
{
int N = 3 ;
System.out.print(CtOfNums(N));
}
}
|
Python3
def CtOfNums(N):
res = (N + 1 ) * ( 1 <<(N - 2 ))
return round (res)
if __name__ = = '__main__' :
N = 3
print (CtOfNums(N))
|
C#
using System;
class GFG{
static int CtOfNums( int N)
{
double res = (N + 1) * (1 << (N - 2));
return ( int )Math.Round(res);
}
public static void Main ()
{
int N = 3;
Console.Write(CtOfNums(N));
}
}
|
Javascript
<script>
function CtOfNums(N)
{
var res = (N + 1) * (1 << (N - 2));
return Math.round(res);
}
var N = 3;
document.write(CtOfNums(N));
</script>
|
Time Complexity: O(log2N)
Auxiliary Space: O(1)
Last Updated :
17 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...