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 ≤ 10^{4}
1 ≤ arr[i] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Tree algorithms

Data structures

Starter code: # User function Template for Python3

class Solution:
    def heapHeight(self, N, arr):
        # code here

Solutions:
class Solution:

	def heapHeight(self, N, arr):
		c = 0
		a = 1
		while a <= N:
			a = a * 2
			c += 1
		return c - 1


import math

class Solution:

	def heapHeight(self, N, arr):
		return math.floor(math.log2(N))


import math

class Solution:

	def heapHeight(self, N, arr):
		return math.ceil(math.log2(N + 1)) - 1


import math

class Solution:

	def heapHeight(self, N, arr):
		return math.floor(math.log(N, 2))


import math

class Solution:

	def heapHeight(self, N, arr):
		return int(math.log2(N))


class Solution:

	def find_parent(self, index):
		return (index - 1) / 2

	def find_left(self, index):
		return 2 * index + 1

	def find_right(self, index):
		return 2 * index + 2

	def heapHeight(self, N, arr):
		count = 0
		i = 0
		while i < N:
			count = count + 1
			i = self.find_left(i)
		return count - 1


class Solution:

	def heapHeight(self, N, arr):
		mul = 1
		count = 1
		i = 0
		while i < N - 1:
			if i + mul * 2 < N - 1:
				count = count + 1
			mul = mul * 2
			i = i + mul
		return count


import math

class Solution:

	def heapHeight(self, N, arr):
		return int(math.ceil(math.log(N + 1) / math.log(2) - 1))


import heapq

class Solution:

	def heapHeight(self, N, arr):
		c = 0
		while N != 1:
			N //= 2
			c += 1
		return c


class Solution:

	def heapHeight(self, N, arr):
		leftInd = 0
		level = -1
		while leftInd < N:
			level = level + 1
			leftInd = 2 * leftInd + 1
		return level


import math

class Solution:

	def heapHeight(self, N, arr):
		last_index = N - 1
		height = math.floor(math.log2(last_index + 1))
		return height


class Solution:

	def heapHeight(self, n, a):
		import math
		return int(math.log2(n))


class Solution:

	def heapHeight(self, n, a):
		import math
		count = 0
		for i in range(1, int(math.sqrt(n)) + 1):
			if pow(2, i) <= n:
				count += 1
		return count


class Solution:

	def heapHeight(self, N, arr):
		height = 0
		sum = 0
		if N == 0:
			return 0
		while True:
			sum = sum + 2 ** height
			if sum == N or sum > N:
				return height
			if sum < N:
				height = height + 1


class Solution:

	def heapHeight(self, N, arr):
		height = 0
		while N != 1:
			height += 1
			N //= 2
		return height


class Solution:

	def heapHeight(self, N, arr):
		count = 0
		divisor = 1
		flag = True
		while flag:
			if N // divisor > 1:
				count += 1
			else:
				flag = False
			divisor *= 2
		return count


class Solution:

	def heapHeight(self, N, arr):
		if len(arr) == 1:
			return 0
		i = 1
		j = 1
		res = 0
		while i < N:
			i = i + 2 ** j
			j += 1
			res += 1
		return res


class Solution:

	def heapHeight(self, N, arr):
		i = 1
		N = N - 1
		ht = 0
		while N > 0:
			i = i * 2
			N = N - i
			ht = ht + 1
		return ht


from math import pow

class Solution:

	def heapHeight(self, N, arr):
		c = -1
		i = 0
		while N > 0:
			N = N - pow(2, i)
			i += 1
			c += 1
		return c


import math

class Solution:

	def heapHeight(self, N, arr):
		cnt = 0
		i = 1
		for _ in range(N):
			if i > N:
				break
			i *= 2
			cnt += 1
		return cnt - 1


class Solution:

	def heapHeight(self, N, arr):
		h = 0
		cap = 1
		while cap < N:
			h += 1
			cap = 2 ** h - 1
		return h - 1


class Solution:

	def heapHeight(self, N, arr):
		k = 1
		while k < N:
			if N >= 2 ** k:
				k += 1
				continue
			else:
				if k - 1 == 0:
					return k
				return k - 1


class Solution:

	def heapHeight(self, N, arr):
		length = len(arr)
		count = 0
		while length != 1:
			length = length // 2
			count += 1
		return count


class Solution:

	def heapHeight(self, N, arr):
		curr_ind = N - 1
		count = 0
		while curr_ind != 0:
			curr_ind = (curr_ind - 1) // 2
			count += 1
		return count


class Solution:

	def heapHeight(self, N, arr):
		del arr
		i = 0
		c = 1
		while N > c:
			N -= c
			i += 1
			c *= 2
		return i


class Solution:

	def heapHeight(self, N, arr):
		s = 2 ** 0
		for i in range(N):
			if s >= N:
				return i
			s = s + 2 ** (i + 1)


class Solution:

	def heapHeight(self, N, arr):
		for i in range(N):
			if 2 ** i > N:
				return i - 1
			elif 2 ** i == N:
				return i


class Solution:

	def heapHeight(self, N, arr):
		values = [2 ** i for i in range(20)]
		summa = 0
		i = 0
		for item in values:
			summa += item
			if summa >= N:
				return i
			i += 1


import math

class Solution:

	def heapHeight(self, N, arr):
		x = 1
		res = -1
		while x <= N:
			x *= 2
			res += 1
		return res


class Solution:

	def heapHeight(self, n, arr):
		i = 0
		h = 0
		while i < n:
			l = 2 * i + 1
			if l < n:
				h += 1
				i = l
			else:
				return h


import math

class Solution:

	def heapHeight(self, N, arr):
		i = 0
		while True:
			if N <= math.pow(2, i) - 1:
				break
			else:
				i += 1
		return i - 1


import math

class Solution:

	def heapHeight(self, N, arr):
		cnt = -1
		i = 0
		while N > 0:
			N = N - int(math.pow(2, i))
			i += 1
			cnt += 1
		return cnt


class Solution:

	def heapHeight(self, N, arr):
		i = 0
		j = 0
		c = 0
		while i < N:
			c = c + 1
			i = i + pow(2, j)
			j = j + 1
		return c - 1


import math

class Solution:

	def heapHeight(self, N, arr):
		h = 0
		i = 0
		p = 0
		while i < N:
			i += math.pow(2, p)
			p += 1
			h += 1
		return h - 1


import math

class Solution:

	def heapHeight(self, N, arr):
		counter = 0
		index = 1
		if len(arr) == 2:
			return 1
		while index * 2 <= N:
			counter += 1
			index = index * 2
		return counter


class Solution:

	def heapHeight(self, N, arr):
		num = 2
		left = N - 1
		count = 0
		while left != 0:
			count += 1
			if num < left or num == left:
				left = left - num
			elif num > left:
				left = 0
			num = num * 2
		return count


class Solution:

	def heapHeight(self, N, arr):
		i = 0
		while 1:
			if 2 ** i <= N:
				k = i
			else:
				break
			i += 1
		return k