Given are the heights of certain Buildings which lie adjacent to each other. Sunlight starts falling from the left side of the buildings. If there is a building of a certain Height, all the buildings to the right side of it having lesser heights cannot see the sun. The task is to find the total number of such buildings that receive sunlight.
 
Example 1:
Input:
N = 6
H[] = {6, 2, 8, 4, 11, 13}
Output:
4
Explanation:
Only buildings of height 6, 8, 11 and
13 can see the sun, hence output is 4.
 
Example 2:
Input:
N = 5
H[] = {2, 5, 1, 8, 3}
Output:
3
Explanation:
Only buildings of height 2, 5 and 8
can see the sun, hence output is 3.
 
Example 3:
Input:
N = 7
H[] = {3, 4, 1, 0, 6, 2, 3}
Output:
3
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function longest() which takes the array A[] and its size N as inputs and returns the desired output.
 
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ H_{i} ≤ 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Starter code: #User function Template for python3

class Solution:
    def longest(self, arr, n):
        #Code Here
    


Solutions:
class Solution:

	def longest(self, arr, n):
		list1 = arr[0]
		count1 = 1
		for i in range(1, n):
			if arr[i] >= list1:
				list1 = arr[i]
				count1 += 1
		return count1


class Solution:

	def longest(self, arr, n):
		max = arr[0]
		count = 1
		for i in range(1, n):
			if arr[i] >= max:
				max = arr[i]
				count = count + 1
		return count


class Solution:

	def longest(self, arr, n):
		mx = arr[0]
		cnt = 1
		for i in range(1, n):
			if arr[i] >= mx:
				mx = arr[i]
				cnt = cnt + 1
		return cnt


class Solution:

	def longest(self, arr, n):
		height = arr[0]
		c = 1
		for i in range(1, n):
			if arr[i] >= height:
				c += 1
				height = arr[i]
		return c


class Solution:

	def longest(self, arr, n):
		count = 1
		curr_max = arr[0]
		for i in range(1, n):
			if arr[i] > curr_max or arr[i] == curr_max:
				count += 1
				curr_max = arr[i]
		return count


class Solution:

	def longest(self, arr, n):
		st = []
		ans = 0
		for i in arr:
			if st == []:
				st.append(i)
				ans += 1
			elif i >= st[-1]:
				st.append(i)
				ans += 1
		return ans


class Solution:

	def longest(self, arr, n):
		ans = 1
		MAX = arr[0]
		for i in range(1, n):
			if arr[i] >= MAX:
				ans += 1
				MAX = arr[i]
			else:
				continue
		return ans


class Solution:

	def longest(self, arr, n):
		count = 0
		leftmax = []
		maxheight = 0
		for i in arr:
			if i > maxheight:
				maxheight = i
			leftmax.append(maxheight)
		for i in range(n):
			if arr[i] >= leftmax[i]:
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		i = 1
		count = 1
		maxx = arr[0]
		while i < n:
			if arr[i] >= maxx:
				count += 1
				maxx = arr[i]
			i += 1
		return count


class Solution:

	def longest(self, arr, n):
		mx_count = 0
		mx_elem = arr[0]
		for elem in arr:
			if elem >= mx_elem:
				mx_count += 1
				mx_elem = elem
		return mx_count


class Solution:

	def longest(self, arr, n):
		cnt = 0
		maxx = 0
		for i in arr:
			if i >= maxx:
				maxx = i
				cnt += 1
		return cnt


class Solution:

	def longest(self, arr, n):
		b = []
		x = arr[0]
		for i in range(n):
			if x <= arr[i]:
				x = arr[i]
				b.append(x)
			else:
				pass
		return len(b)


class Solution:

	def longest(self, arr, n):
		a = arr[0]
		b = []
		for i in range(len(arr)):
			if a <= arr[i]:
				a = arr[i]
				b.append(a)
			else:
				continue
		return len(b)


class Solution:

	def longest(self, arr, n):
		count = 1
		x = arr[0]
		y = []
		for i in range(n):
			if x <= arr[i]:
				x = arr[i]
				y.append(x)
				count = count + 1
			else:
				pass
		return len(y)


class Solution:

	def longest(self, arr, n):
		m = []
		c = 0
		for i in range(len(arr)):
			if arr[i] >= c:
				c = arr[i]
				m.append(c)
		return len(m)


class Solution:

	def longest(self, arr, n):
		if len(set(arr)) == 1:
			return n
		c = 1
		mx = arr[0]
		for i in range(1, n):
			if arr[i] >= mx:
				c += 1
				mx = arr[i]
		return c


class Solution:

	def longest(self, arr, n):
		max1 = 0
		res = 0
		for i in arr:
			if i >= max1:
				max1 = i
				res += 1
		return res


class Solution:

	def longest(self, arr, n):
		if n == 2:
			if arr[0] > arr[1]:
				return 1
			else:
				return 2
		max = arr[0]
		a = 0
		for i in range(n):
			if arr[i] >= max:
				max = arr[i]
				a = a + 1
		return a


class Solution:

	def longest(self, arr, n):
		a = []
		a.append(arr[0])
		for i in range(1, n):
			if a[-1] <= arr[i]:
				a.append(arr[i])
		return len(a)


class Solution:

	def longest(self, arr, n):
		count = 0
		highest = 0
		for building in arr:
			if building >= highest:
				count += 1
				highest = building
		return count


class Solution:

	def longest(self, arr, n):
		c = 1
		sta = arr[0]
		for i in range(1, n):
			if arr[i] >= sta:
				c += 1
				sta = max(arr[i], sta)
		return c


class Solution:

	def longest(self, arr, n):
		curmax = arr[0]
		nofv = 1
		for i in range(1, n):
			if arr[i] >= curmax:
				nofv += 1
				curmax = arr[i]
		return nofv


class Solution:

	def longest(self, arr, n):
		ans = 1
		temp = arr[0]
		for i in range(1, n):
			height = arr[i]
			if height >= temp:
				temp = height
				ans += 1
		return ans


class Solution:

	def longest(self, arr, n):
		maxi = arr[0]
		count = 1
		for i in range(1, len(arr)):
			if arr[i] >= maxi:
				maxi = arr[i]
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		arr2 = []
		arr2.append(arr[0])
		prv = arr[0]
		for i in range(1, n):
			if arr[i] > prv or arr[i] == prv:
				arr2.append(arr[i])
				prv = arr[i]
		return len(arr2)


class Solution:

	def longest(self, arr, n):
		c = 0
		maxi = -float('inf')
		for x in arr:
			if x >= maxi:
				maxi = x
				c += 1
		return c


class Solution:

	def longest(self, arr, n):
		e = arr.pop(0)
		count = 1
		for v in arr:
			if v >= e:
				e = v
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		(temp, count) = (0, 0)
		for (i, v) in enumerate(arr):
			if v >= temp:
				temp = v
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		m = 0
		c = 0
		for i in arr:
			if i >= m:
				m = i
				c += 1
		return c


class Solution:

	def longest(self, arr, n):
		m = arr[0]
		p = 1
		for i in range(1, n):
			if m <= arr[i]:
				m = max(m, arr[i])
				p += 1
		return p


class Solution:

	def longest(self, arr, n):
		max = -1
		a = 0
		for i in arr:
			if max <= i:
				a += 1
				max = i
		return a


class Solution:

	def longest(self, a, n):
		x = a[0]
		count = 1
		for i in range(1, n):
			if a[i] >= x:
				x = a[i]
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		mx = -1
		count = 0
		for i in arr:
			if i >= mx:
				mx = i
				count += 1
		return count


class Solution:

	def longest(self, arr, n):
		count = 0
		prevMax = arr[0]
		for height in arr:
			if height >= prevMax:
				count += 1
				prevMax = height
		return count


class Solution:

	def longest(self, arr, n):
		max_building = arr[0]
		count = 1
		for i in range(1, n):
			if arr[i] >= max_building:
				count += 1
				max_building = arr[i]
		return count
x = Solution()
arr = [6, 2, 8, 4, 11, 13]
x.longest(arr, len(arr))


class Solution:

	def longest(self, arr, n):
		maxi = arr[0]
		ans = 0
		ans = ans + 1
		for i in range(1, n):
			if arr[i] >= maxi:
				maxi = arr[i]
				ans = ans + 1
		return ans


class Solution:

	def longest(self, arr, n):
		ans = [0]
		i = 1
		while i < n:
			if arr[i] >= arr[ans[-1]]:
				ans.append(i)
			i += 1
		return len(ans)


class Solution:

	def longest(self, arr, n):
		res = []
		count = 1
		res.append(arr[0])
		for i in range(1, len(arr)):
			if arr[i] >= res[-1]:
				res.append(arr[i])
				count = count + 1
			else:
				res.append(res[-1])
		return count


class Solution:

	def longest(self, arr, n):
		h = arr[0]
		sum = 1
		for i in range(1, len(arr)):
			if h <= arr[i]:
				h = arr[i]
				sum += 1
		return sum


class Solution:

	def longest(self, arr, n):
		mx = -1
		cnt = 0
		for e in arr:
			if e >= mx:
				mx = e
				cnt += 1
		return cnt