Given heights of consecutive buildings, find the maximum number of consecutive steps one can put forward such that he gain a increase in altitude while going from roof of one building to next adjacent one.
Examples :
Input : arr[] = {1, 2, 2, 3, 2}
Output : 1
Explanation :
Maximum consecutive steps from 1 to 2 OR 2 to 3.
Input : arr[] = {1, 2, 3, 4}
Output : 3

This problem is basically a variation of Longest increasing subarray
Approach:-
initialize count = 0
initialize maximum = 0
if arr[i]>a[i-1]
then count increment
else
maximum = max(maximum, count)
at the end maximum=max(maximum, count)
C++
// CPP code to find maximum // number of consecutive steps. #include <bits/stdc++.h> using namespace std; // Function to count consecutive steps int find_consecutive_steps(int arr[], int len) { int count = 0; int maximum = 0; for (int index = 1; index < len; index++) { // count the number of consecutive // increasing height building if (arr[index] > arr[index - 1]) count++; else { maximum = max(maximum, count); count = 0; } } return max(maximum, count); } // Driver code int main() { int arr[] = { 1, 2, 3, 4 }; int len = sizeof(arr) / sizeof(arr[0]); cout << find_consecutive_steps(arr, len); } |
Java
// Java code to find maximum // number of consecutive steps. import java.io.*; class GFG { // Function to count consecutive steps static int find_consecutive_steps(int arr[], int len) { int count = 0; int maximum = 0; for (int index = 1; index < len; index++) { // count the number of consecutive // increasing height building if (arr[index] > arr[index - 1]) count++; else { maximum = Math.max(maximum, count); count = 0; } } return Math.max(maximum, count); } // Driver code public static void main (String[] args) { int arr[] = { 1, 2, 3, 4 }; int len = arr.length; System.out.println(find_consecutive_steps(arr, len)); } } // This code is contributed by Gitanjali. |
Python3
# Python3 code to find maximum # number of consecutive steps import math # Function to count consecutive steps def find_consecutive_steps(arr, len): count = 0; maximum = 0 for index in range(1, len): # count the number of consecutive # increasing height building if (arr[index] > arr[index - 1]): count += 1 else: maximum = max(maximum, count) count = 0 return max(maximum, count) # Driver code arr = [ 1, 2, 3, 4 ] len = len(arr) print(find_consecutive_steps(arr, len)) # This code is contributed by Gitanjali. |
C#
// C# code to find maximum // number of consecutive steps. using System; class GFG { // Function to count consecutive steps static int find_consecutive_steps(int []arr, int len) { int count = 0; int maximum = 0; for (int index = 1; index < len; index++) { // count the number of consecutive // increasing height building if (arr[index] > arr[index - 1]) count++; else { maximum = Math.Max(maximum, count); count = 0; } } return Math.Max(maximum, count); } // Driver code public static void Main () { int []arr = { 1, 2, 3, 4 }; int len = arr.Length; Console.WriteLine(find_consecutive_steps(arr, len)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP code to find maximum // number of consecutive steps. // Function to count // consecutive steps function find_consecutive_steps($arr, $len) { $count = 0; $maximum = 0; for ($index = 1; $index < $len; $index++) { // count the number of consecutive // increasing height building if ($arr[$index] > $arr[$index - 1]) $count++; else { $maximum = max($maximum, $count); $count = 0; } } return max($maximum, $count); } // Driver code $arr = array( 1, 2, 3, 4 ); $len = count($arr); echo find_consecutive_steps($arr, $len); // This code is contributed by vt_m. ?> |
Output :
3
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Steps to return to {1, 2, ..n} with specified movements
- Find the Alpha Score of the Given Steps (Using BST)
- Generate array with minimum sum which can be deleted in P steps
- Minimum steps required to reach the end of a matrix | Set 2
- Count minimum steps to get the given desired array
- Minimum steps to reach end of array under constraints
- Number of steps to convert to prime factors
- Minimum steps for increasing and decreasing Array to reach either 0 or N
- Minimize steps to reach K from 0 by adding 1 or doubling at each step
- Minimum steps to make sum and the product of all elements of array non-zero
- Find index after traversing a permutation Array of 1 to N by K steps
- Minimize the number of steps required to reach the end of the array | Set 2
- Minimum steps required to make an array decreasing
- Minimum steps to convert an Array into permutation of numbers from 1 to N
- Min steps to empty an Array by removing a pair each time with sum at most K
- Minimum steps to make all the elements of the array divisible by 4
- Minimum steps required to reduce all the elements of the array to zero
- Minimize the number of steps required to reach the end of the array
- Minimum steps to make the product of the array equal to 1
- Minimize Steps required to obtain Sorted Order of an Array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m

