You are given heights of consecutive buildings. You can move from the roof of a building to the roof of next adjacent building. You need to find the maximum number of consecutive steps you can put forward such that you gain an increase in altitude with each step.
Example 1:
Input:
N = 5
A[] = {1,2,2,3,2}
Output: 1
Explanation: 1, 2 or 2, 3 are the only consecutive 
buildings with increasing heights thus maximum number
of consecutive steps with increase in gain in altitude
would be 1 in both cases.
Example 2:
Input:
N = 4
A[] = {1,2,3,4}
Output: 3
Explanation: 1 to 2 to 3 to 4 is the jump of
length 3 to have maximum number of 
buildings with increasing heights, so maximum steps
with increasing altitude becomes 3.
Your Task:
The task is to complete the function maxStep() which takes an array A[] (denoting the heights of buildings) and its size N as inputs and returns the maximum number of steps to gain increase in altitude.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1). 
Constraints:
1 <= N <= 10^{6}
1 <= A_{i }<= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3



class Solution:

    

    #Function to find maximum number of consecutive steps 

    #to gain an increase in altitude with each step.

    def maxStep(self,A, N):

        #Your code here

Solutions:
class Solution:

	def maxStep(self, A, N):
		i = 1
		c = 0
		max1 = 0
		while i < N:
			if A[i] - A[i - 1] > 0:
				c += 1
			else:
				max1 = max(max1, c)
				c = 0
			i += 1
		return max(max1, c)


class Solution:

	def maxStep(self, A, N):
		ms = 0
		c = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				c += 1
			else:
				c = 0
			if ms < c:
				ms = c
		return ms


class Solution:

	def maxStep(self, A, N):
		maxi = 0
		count = 0
		for x in range(N - 1):
			if A[x + 1] > A[x]:
				count += 1
				maxi = max(count, maxi)
			elif A[x + 1] <= A[x]:
				count = 0
		return maxi


class Solution:

	def maxStep(self, A, N):
		k = 0
		res = 0
		for i in range(1, len(A)):
			if A[i - 1] < A[i]:
				k += 1
			else:
				res = max(res, k)
				k = 0
		res = max(res, k)
		return res


class Solution:

	def maxStep(self, A, N):
		maxi = 0
		curr = 0
		if N == 1:
			return 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				maxi += 1
				if maxi > curr:
					curr = maxi
			else:
				maxi = 0
		return curr


class Solution:

	def maxStep(self, A, N):
		max1 = 0
		max_o = max1
		for i in range(N - 1):
			diff = A[i + 1] - A[i]
			if diff >= 1:
				max1 = max1 + 1
			else:
				max1 = 0
			max_o = max(max1, max_o)
		return max_o


class Solution:

	def maxStep(self, a, n):
		all_steps = []
		steps = 0
		for i in range(1, n):
			if a[i - 1] < a[i]:
				steps = max(steps, 1)
				if i >= 2:
					if a[i - 2] < a[i - 1]:
						steps += 1
			else:
				steps = 0
			all_steps.append(steps)
		if all_steps:
			return max(all_steps)
		else:
			return 0


class Solution:

	def maxStep(self, A, N):
		cr_lt = 0
		mx_lt = 0
		for i in range(1, N):
			if A[i - 1] < A[i]:
				cr_lt = cr_lt + 1
				mx_lt = max(mx_lt, cr_lt)
			else:
				cr_lt = 0
		return mx_lt


class Solution:

	def maxStep(self, A, N):
		no_of_steps = 0
		maxs = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				no_of_steps += 1
			else:
				maxs = max(maxs, no_of_steps)
				no_of_steps = 0
		maxs = max(maxs, no_of_steps)
		return maxs


class Solution:

	def maxStep(self, arr, N):
		max_step = 0
		curr_step = 0
		for i in range(N - 1):
			if arr[i + 1] - arr[i] > 0:
				curr_step += 1
			else:
				curr_step = 0
			max_step = max(curr_step, max_step)
			if curr_step < 0:
				curr_step = 0
		return max_step


class Solution:

	def maxStep(self, a, n):
		c = 0
		ans = 0
		for i in range(1, n):
			if a[i] > a[i - 1]:
				c += 1
			else:
				c = 0
			ans = max(ans, c)
		return ans


class Solution:

	def maxStep(self, A, N):
		l = [0]
		j = 1
		c = 0
		arr = A
		while j < N:
			if arr[j - 1] < arr[j]:
				c += 1
			else:
				l.append(c)
				c = 0
			j += 1
		l.append(c)
		return max(l)


class Solution:

	def maxStep(self, A, N):
		c = 0
		d = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				c += 1
				d = max(c, d)
			elif A[i] <= A[i - 1]:
				d = max(c, d)
				c = 0
		return d


class Solution:

	def maxStep(self, A, N):
		if N == 1:
			return 0
		list_step = []
		count_sum = 0
		for (count, i) in enumerate(A[1:]):
			if i > A[count]:
				count_sum += 1
			else:
				list_step.append(count_sum)
				count_sum = 0
		if count_sum != 0:
			list_step.append(count_sum)
		return max(list_step)


def roofTop(arr, n):
	total = 0
	res = 0
	for i in range(n - 1):
		if arr[i] < arr[i + 1]:
			total += 1
		else:
			res = max(res, total)
			total = 0
	res = max(res, total)
	return res

class Solution:

	def maxStep(self, A, N):
		return roofTop(A, N)


class Solution:

	def maxStep(self, A, N):
		if N == 1:
			return 0
		ans = []
		ct = 0
		for i in range(0, len(A) - 1):
			if A[i] < A[i + 1]:
				ct = ct + 1
			if A[i] >= A[i + 1]:
				ans.append(ct)
				ct = 0
		ans.append(ct)
		return max(ans)


class Solution:

	def maxStep(self, A, N):
		max_steps = 0
		current_steps = 0
		for i in range(1, len(A)):
			if A[i] > A[i - 1]:
				current_steps += 1
				max_steps = max(max_steps, current_steps)
			else:
				current_steps = 0
		return max_steps


class Solution:

	def maxStep(self, A, N):
		l = []
		c = 0
		if len(A) == 1:
			return 0
		for i in range(len(A) - 1):
			if A[i + 1] > A[i]:
				c += 1
				l.append(c)
			else:
				c = 0
				l.append(c)
		return max(l)


class Solution:

	def maxStep(self, A, N):
		if len(A) == 1:
			return 0
		max_cnt = 0
		prev = A[0]
		cnt = 0
		for i in range(1, len(A)):
			curr = A[i]
			if curr - prev > 0:
				cnt += 1
				max_cnt = max(max_cnt, cnt)
			else:
				cnt = 0
			prev = curr
		return max_cnt


class Solution:

	def maxStep(self, A, N):
		(max_jumps, current_jumps) = (0, 0)
		for i in range(1, N):
			if A[i] > A[i - 1]:
				current_jumps += 1
				max_jumps = max(max_jumps, current_jumps)
			else:
				current_jumps = 0
		return max_jumps


class Solution:

	def maxStep(self, A, N):
		count = 0
		l = []
		if N == 2:
			for i in range(N - 1):
				if A[i] < A[i + 1]:
					count += 1
			return count
		elif N == 1 or N == 0:
			return 0
		else:
			for i in range(N - 1):
				if A[i] < A[i + 1]:
					count += 1
				else:
					l.append(count)
					count = 0
			if count != 0 or len(l) != 0:
				if max(l) >= count:
					return max(l)
				else:
					return count
			else:
				return 0


class Solution:

	def maxStep(self, A, N):
		(n1, n2) = (0, 0)
		e = A[0]
		for i in range(1, N):
			if e < A[i]:
				n1 += 1
				e = A[i]
			elif e >= A[i]:
				n2 = max(n2, n1)
				n1 = 0
				e = A[i]
		return max(n1, n2)


class Solution:

	def maxStep(self, arr, N):
		curr = 0
		nextt = 1
		maxx = 0
		while nextt < N:
			if arr[nextt] > arr[curr] and arr[nextt] > arr[nextt - 1]:
				if nextt - curr > maxx:
					maxx = nextt - curr
				nextt += 1
			else:
				curr = nextt
				nextt += 1
		return maxx


class Solution:

	def maxStep(self, arr, N):
		ans = 0
		c = 0
		for i in range(1, N):
			if arr[i] - arr[i - 1] > 0:
				c += 1
			else:
				c = 0
			ans = max(c, ans)
		return ans


class Solution:

	def maxStep(self, arr, n):
		t = 0
		a = [0]
		for i in range(0, n - 1):
			if arr[i] < arr[i + 1]:
				t = t + 1
				a.append(t)
			else:
				t = 0
		return max(a)


class Solution:

	def maxStep(self, A, N):
		maxx = 1
		curr = 1
		for i in range(1, N):
			if A[i] > A[i - 1]:
				curr += 1
				maxx = max(maxx, curr)
			else:
				curr = 1
		return maxx - 1


class Solution:

	def maxStep(self, A, N):
		(pre, ans, L) = (A[0], 0, 0)
		for v in A:
			if v > pre:
				L += 1
				ans = max(ans, L)
			else:
				L = 0
			pre = v
		return ans


class Solution:

	def maxStep(self, A, N):
		max_steps = 1
		curr_steps = 1
		for i in range(1, N):
			if A[i] > A[i - 1]:
				curr_steps += 1
				max_steps = max(max_steps, curr_steps)
			else:
				curr_steps = 1
		return max_steps - 1


class Solution:

	def maxStep(self, A, N):
		steps = 0
		maximum = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				steps += 1
			else:
				maximum = max(maximum, steps)
				steps = 0
		maximum = max(maximum, steps)
		return maximum


class Solution:

	def maxStep(self, a, n):
		count = 0
		maxs = 0
		for i in range(n - 1):
			if a[i] < a[i + 1]:
				count += 1
			else:
				maxs = max(count, maxs)
				count = 0
		maxs = max(maxs, count)
		return maxs


class Solution:

	def maxStep(self, A, N):
		if N == 1:
			return 0
		c = 0
		m = 0
		j = 0
		while j < N - 1:
			if A[j + 1] > A[j]:
				c = c + 1
			else:
				m = max(c, m)
				c = 0
			j = j + 1
		return max(c, m)


class Solution:

	def maxStep(self, A, N):
		ans = 0
		cur = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				cur += 1
				ans = max(cur, ans)
			else:
				cur = 0
		return ans


class Solution:

	def maxStep(self, A, N):
		count = 0
		maxm = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				count += 1
			else:
				maxm = max(maxm, count)
				count = 0
		maxm = max(maxm, count)
		return maxm


class Solution:

	def maxStep(self, A, N):
		steps = max_steps = 0
		x = A[0]
		for i in range(1, N):
			if A[i] > x:
				steps += 1
			else:
				if steps > max_steps:
					max_steps = steps
				steps = 0
			x = A[i]
		if steps > max_steps:
			max_steps = steps
		return max_steps


class Solution:

	def maxStep(self, A, N):
		mx = 0
		i = 0
		count = 0
		while i < N - 1:
			if A[i + 1] - A[i] >= 1:
				count += 1
			else:
				mx = max(count, mx)
				count = 0
			i += 1
		mx = max(count, mx)
		return mx


class Solution:

	def maxStep(self, A, N):
		i = 0
		ans = 0
		j = 1
		while j < len(A):
			if A[j] > A[i] and A[j] > A[j - 1]:
				ans = max(ans, j - i)
			else:
				i = j
			j += 1
		return ans


class Solution:

	def maxStep(self, A, N):
		if N == 1:
			return 0
		ste = 0
		maxse = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				ste += 1
				if ste > maxse:
					maxse = ste
			else:
				ste = 0
		return maxse


class Solution:

	def maxStep(self, A, N):
		cur_len = 1
		res = 1
		for i in range(N - 1):
			if A[i + 1] > A[i]:
				cur_len += 1
				if cur_len > res:
					res = cur_len
			else:
				cur_len = 1
		return res - 1


class Solution:

	def maxStep(self, arr, N):
		steps_taken = 0
		height = 0
		for i in range(1, N):
			if arr[i - 1] < arr[i]:
				steps_taken += 1
				height = max(height, steps_taken)
			else:
				steps_taken = 0
		return height


class Solution:

	def maxStep(self, A, N):
		n_steps = 0
		m = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				n_steps += 1
			else:
				m = max(m, n_steps)
				n_steps = 0
		m = max(m, n_steps)
		return m


class Solution:

	def maxStep(self, A, N):
		x = []
		y = 0
		for i in range(1, N):
			if A[i - 1] < A[i]:
				y = y + 1
				x.append(y)
			else:
				y = 0
		if len(x) == 0:
			return 0
		return max(x)


class Solution:

	def maxStep(self, A, N):
		s = 0
		c = 0
		if N < 2:
			return 0
		for i in range(N - 1):
			if A[i + 1] - A[i] > 0:
				c += 1
				s = max(s, c)
			else:
				c = 0
		return s


class Solution:

	def maxStep(self, A, N):
		max_hei = 0
		add_sum = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				add_sum = add_sum + 1
			else:
				max_hei = max(max_hei, add_sum)
				add_sum = 0
		return max(max_hei, add_sum)


class Solution:

	def maxStep(self, A, N):
		prev_height = -1
		max_step = 0
		curr_steps = -1
		for h in A:
			if h > prev_height:
				curr_steps += 1
				max_step = max(curr_steps, max_step)
			else:
				max_step = max(curr_steps, max_step)
				curr_steps = 0
			prev_height = h
		return max_step


class Solution:

	def maxStep(self, A, N):
		from math import inf
		if N == 1:
			return 0
		max_count = 0
		count = 0
		for i in range(1, N):
			if A[i] > A[i - 1]:
				count += 1
			elif A[i] <= A[i - 1]:
				count = 0
			max_count = max(max_count, count)
		return max_count


class Solution:

	def maxStep(self, A, N):
		max_step = step = 0
		for i in range(N - 1):
			if A[i] < A[i + 1]:
				step += 1
			else:
				max_step = max(max_step, step)
				step = 0
		return max(max_step, step)


class Solution:

	def maxStep(self, a, N):
		count = 0
		res = 0
		for i in range(1, N):
			if a[i] > a[i - 1]:
				count += 1
				res = max(res, count)
			else:
				count = 0
		return res


class Solution:

	def maxStep(self, A, N):
		ans = 0
		i = 0
		while i < N - 1:
			cnt = 0
			while i < N - 1 and A[i] < A[i + 1]:
				cnt += 1
				i += 1
			ans = max(cnt, ans)
			i += 1
		return ans