Height of Heap | Practice | GeeksforGeeks

Given a Binary Heap of size N in an array arr[]. Write a program to calculate the height of the Heap.
Example 1:
Input: N = 6
arr = {1, 3, 6, 5, 9, 8}
Output: 2
Explaination: The tree is like the following
           (1)
       /   \
    (3)    (6)
    / \     /
  (5) (9) (8)
Example 2:
Input: N = 9
arr = {3, 6, 9, 2, 15, 10, 14, 5, 12}
Output: 3
Explaination: The tree looks like following
               (2)
        /      \
      (3)      (9)
     /  \     /   \
   (5) (15) (10) (14)
   / \
 (6) (12)
Your Task:
You do not need to read input or print anything. Your task is to complete the function heapHeight() which takes the value N and the array arr as input parameters and returns the height of the heap.
Expected Time Complexity: O(logN)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 104
1 ≤ arr[i] ≤ 106
0
nikhilnaidu232 days ago
0.17 time with space complexity  O(1) by just using N
 
int heapHeight(int N, int arr[]){       // code here       int len=1;       if(N==1)           return 0;       else if(N==2 or N==3)           return 1;       else if(N==4)           return 2;       else               for(int i=2;i<N;i=i*2) // i is the number of nodes as the tree grows               if(N-i>i)  //Checking if the number of nodes doesn't exceed the N                    len++; //Incrementing the length                      return len;       }
0
hharshit81182 weeks ago
C++ Solution :
 int heapHeight(int N, int arr[]){       return log2(N);   }
O(1) time complexity.
0
sakshamvedi1 month ago
 
JAVA SOLUTION
 
class Solution{   static int heapHeight(int N, int arr[]){       return (int)Math.ceil(Math.log(N +                   1) / Math.log(2)) - 1;   }}
0
0niharika21 month ago
return log2(N);
0
rahulsoni23032 months ago
import math

class Solution:
    def heapHeight(self, N, arr):
        return math.floor(math.log(N, 2))
+2
saumyatupsi2 months ago
class Solution{public:   int heapHeight(int N, int arr[]){              return floor(log2(N));          }}; 
0
nekhatperveen2 months ago
JAVA
  static int heapHeight(int N, int arr[])
{            if(N==0)       return 0;       int count=0;       while(N>=1)       {           N=N/2;           count++;       }       return count-1;   }
0
brahmareddyaakumaiia3 months ago
simple cpp solution
Time Taken:0.2/2.2
#include<cmath>class Solution{public:   int heapHeight(int N, int arr[]){       // code here       return log2(N);   }}; 
0
amiransarimy3 months ago
Python Solutions
Total Time Taken:
0.2/1.5

import math
class Solution:
   def heapHeight(self, N, arr):
       return math.ceil(math.log2(N+1)) -1
0
keshrishivam41143 months ago
import mathclass Solution:   def heapHeight(self, N, arr):       return math.ceil(math.log2(N+1))-1
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.