Maximise consecutive steps one can put forward on roof to gain an increase in altitude


In this article our aim is to determine the most steps one can take in a row, provided the heights of the adjacent buildings, to gain altitude as he moves in from roof of one to the roof of the adjacent building. That is building heights for each one after another are provided. You can proceed from one building's roof to the next one down the block. You must determine how many consecutive steps you could indeed take in a row while still gaining altitude with each one.

Problem Statement

Maximize consecutive steps one can put forward on the roof to gain an increase in altitude.

Approach

In the given integer array, that is the array where all the building heights are given in order, we check if the first element is less than the second element. In a clearer way, the height of the first building is less than the height of the second building or the adjacent building. If the first element or the height of the first building is less than the second element or the height of the second building, we take 1 step. If the first element or the height of the first building is not less than the second element or the height of the second building, then we ignore it.

Likewise, the height of every building is compared with the height of the next building adjacent to it.

That is each element in the given array is checked with the value of the very next element or the adjacent element in that array. If the considered element is less than the next element, we count the steps. Otherwise, if the considered element is not less than the next element, then we ignore it.

Algorithm

The algorithm to solve the problem of Maximizing consecutive steps one can put forward on the roof to gain an increase in altitude is given below.

Step 1: Start with a count of 0.

Step 2: Set the maximum value to 0

Step 3: Increase count by 1 if a[i] > a[i-1]

Otherwise, maximum = max (maximum, count), and the count is set to zero.

Step 4: Finally, return the maximum.

Step 5: Stop

Sample Input Outputs

Input

Integer array, a {2,4,6,8,10}

Output

4

Explanation

2$\mathrm{\rightarrow}$4 1 step

4$\mathrm{\rightarrow}$6 1 step

6$\mathrm{\rightarrow}$8 1 step

8$\mathrm{\rightarrow}$10 1 step

Total steps = 4

That is from 2$\mathrm{\rightarrow}$4,1 step plus from 4$\mathrm{\rightarrow}$6, 1 step plus from 6$\mathrm{\rightarrow}$8,1 step and 8$\mathrm{\rightarrow}$10 1 step. So total 4 steps.

Input

Integer array, a {2,4,3,8}

Output

2

Explanation

2$\mathrm{\rightarrow}$4 1 step

4$\mathrm{\rightarrow}$3 not counted since it is a downfall

3$\mathrm{\rightarrow}$8 1 step

Total steps = 1

That is from 2$\mathrm{\rightarrow}$4, 1 step OR 3$\mathrm{\rightarrow}$8 1 step.

Input

Integer array, a {2,4,4,3}

Output

1

Explanation

2$\mathrm{\rightarrow}$4 1 step

4$\mathrm{\rightarrow}$4 not counted since it is not a gain

4$\mathrm{\rightarrow}$3 not counted since it is a downfall

Total steps = 1

That is from 2$\mathrm{\rightarrow;}$4, 1 step.

C program to find the Maximum consecutive steps one can put forward on the roof to gain an increase in altitude is given below.

Example

#include <stdio.h>
#include<math.h>
int consecutive_steps(int a[], int l){
	int count = 0;
	int maximum= 0;
   #define max
	for (int i = 1; i < l; i++) {
		// count the number of consecutive
		if (a[i] > a[i - 1])
			count++;
		else {
			maximum = max(maximum, count);
			count = 0;
		}
	}
	return max(maximum, count);
}
int main()
{
	int a[] = { 2, 4, 6, 8, 10};
	int l = sizeof(a) / sizeof(a[0]);
	printf("%d",consecutive_steps (a, l));
}

Output

4

Conclusion

Likewise, we can find the maximum consecutive steps one can put forward on the roof to gain an increase in altitude by inputting the array values.

The challenge of obtaining the Maximum consecutive steps one can put forward on the roof to gain an increase in altitude is resolved in this article. Here the C programming codes to find the maximum consecutive steps one can put forward on the roof to gain an increase in altitude inputting the array values are provided.

Updated on: 23-Aug-2023

62 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements