int numberOfNodes(int N){
return pow(2,N-1);
}Problem of the day
Given an integer 'N', determine the maximum number of nodes present on 'Nth' level in a binary tree.
For Example:
N = 3
For level 1, we have 1 node.
For level 2, we have 2 nodes.
For level 3, we have 4 nodes
Output = 4, as level 3 has maximum 4 nodes.

The first line of each test case contains an integer ‘N’.
The output contains an integer denoting the maximum number of nodes present on 'N'th level in a binary tree.
You don’t need to print anything. Just implement the given function.
2
2
N = 2
For level 1, we have 1 node.
For level 2, we have 2 nodes.
Output = 2, as level 2 has maximum 2 nodes.

4
8
N = 4
For level 1, we have 1 node.
For level 2, we have 2 nodes.
For level 3, we have 4 nodes
For level 4, we have 8 nodes
Output = 8, as level 4 has maximum 8 nodes.

1 <= N <= 20
Time Limit: 1 sec
Deduce it mathematically.
Approach:
The approach calculates the maximum number of nodes on level ‘N’ of a binary tree using the formula 2^(N-1).
Algorithm:
O(1)
All the operations performed are constant time operations, hence the time complexity is O(1).
O(1)
No extra space is used. Hence, the space complexity is O(1).
Interview problems
Number Of Nodes | Easy C++ solution O(1)
int numberOfNodes(int N){
return pow(2,N-1);
}Interview problems
Java Solution
import java.util.*;
public class Solution {
public static int numberOfNodes(int N){
// Write your code here.
if(N==0){
return 0;
}
return (int)(Math.pow(2,N-1));
}
}
Interview problems
Java Solution
//One-Line Solution
public class Solution {
public static int numberOfNodes(int N){
return 1<<N-1;
}
}
//Another method Solution
public class Solution {
public static int numberOfNodes(int N){
if(N==0 || N==1){
return N;
}
int noOfNode=1;
for(int i=1;i<N;i++){
noOfNode=noOfNode+noOfNode;
}
return noOfNode;
}
}
Interview problems
Java one liner using bitwise operator
public class Solution {
public static int numberOfNodes(int N){
return 1<<N-1;
}
}
Interview problems
Number of Nodes
int numberOfNodes(int N){
if(N==0){
return 0;
}
return (pow(2,N-1));
}
Interview problems
C++ Solution .
int numberOfNodes(int N){
// Write your code here.
return pow(2,N-1);
}
Interview problems
Different thinking must check once
int numberOfNodes(int N){
if(N==0) {
return 0;
}
if(N==1) {
return 1;
}
if(N==2) {
return 2;
}
int ans = 0;
if(N >= 3) {
ans = pow(2, N) - pow(2,N-1);
}
return ans;
}Interview problems
c++
one line of code int numberOfNodes(int N){
// Write your code here.
return pow(2,N)/2;
}
Interview problems
easy Python code
from typing import *
def numberOfNodes(N : int) -> int:
return 2**(N-1)
if u find the code correct plz upvote..
Interview problems
java one line code
int result = ((int)Math.pow(2,N))/2;
return result;