Maximise consecutive steps one can put forward on roof to gain an increase in altitude
Given the heights of consecutive buildings, find the maximum number of consecutive steps one can put forward such that he gains an increase in altitude while going from the roof of one building to the 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
Consecutive steps to the rooftop by finding the longest increasing subarray:
To solve the problem follow the below idea:
This problem is basically a variation of the Longest increasing subarray problem
Follow the given steps to solve the problem:
- initialize count = 0
- initialize maximum = 0
- if arr[i] > a[i-1], then increment count
- else maximum = max(maximum, count) and set count equal to zero
- At the end return maximum = max(maximum, count)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int find_consecutive_steps( int arr[], int len)
{
int count = 0;
int maximum = 0;
for ( int index = 1; index < len; index++) {
if (arr[index] > arr[index - 1])
count++;
else {
maximum = max(maximum, count);
count = 0;
}
}
return max(maximum, count);
}
int main()
{
int arr[] = { 1, 2, 3, 4 };
int len = sizeof (arr) / sizeof (arr[0]);
cout << find_consecutive_steps(arr, len);
}
|
Java
import java.io.*;
class GFG {
static int find_consecutive_steps( int arr[], int len)
{
int count = 0 ;
int maximum = 0 ;
for ( int index = 1 ; index < len; index++) {
if (arr[index] > arr[index - 1 ])
count++;
else {
maximum = Math.max(maximum, count);
count = 0 ;
}
}
return Math.max(maximum, count);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 };
int len = arr.length;
System.out.println(
find_consecutive_steps(arr, len));
}
}
|
Python3
import math
def find_consecutive_steps(arr, len ):
count = 0
maximum = 0
for index in range ( 1 , len ):
if (arr[index] > arr[index - 1 ]):
count + = 1
else :
maximum = max (maximum, count)
count = 0
return max (maximum, count)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 3 , 4 ]
len = len (arr)
print (find_consecutive_steps(arr, len ))
|
C#
using System;
class GFG {
static int find_consecutive_steps( int [] arr, int len)
{
int count = 0;
int maximum = 0;
for ( int index = 1; index < len; index++) {
if (arr[index] > arr[index - 1])
count++;
else {
maximum = Math.Max(maximum, count);
count = 0;
}
}
return Math.Max(maximum, count);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4 };
int len = arr.Length;
Console.WriteLine(find_consecutive_steps(arr, len));
}
}
|
PHP
<?php
function find_consecutive_steps( $arr ,
$len )
{
$count = 0;
$maximum = 0;
for ( $index = 1; $index < $len ;
$index ++)
{
if ( $arr [ $index ] > $arr [ $index - 1])
$count ++;
else
{
$maximum = max( $maximum , $count );
$count = 0;
}
}
return max( $maximum , $count );
}
$arr = array ( 1, 2, 3, 4 );
$len = count ( $arr );
echo find_consecutive_steps( $arr , $len );
?>
|
Javascript
<script>
function find_consecutive_steps(arr, len)
{
let count = 0;
let maximum = 0;
for (let index = 1; index < len; index++) {
if (arr[index] > arr[index - 1])
count++;
else
{
maximum = Math.max(maximum, count);
count = 0;
}
}
return Math.max(maximum, count);
}
let arr = [ 1, 2, 3, 4 ];
let len = arr.length;
document.write(find_consecutive_steps(arr,len));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
15 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...