# C++ program to count number of stairways and number of steps in each stairways

Suppose we have an array A with n elements. Let, Amal climbs the stairs inside a multi-storey building. Every time he climbs, start counting from 1. For example, if he climbs two stairways with 3 steps and 4 steps, he will speak the numbers like 1, 2, 3, 1, 2, 3, 4. In the array A, the numbers are representing stair numbers said by Amal. We have to count the number of staircase did he climb, also print the number of steps in each stairway.

So, if the input is like A = [1, 2, 3, 1, 2, 3, 4, 5], then the output will be 2, [3, 5]

## Steps

To solve this, we will follow these steps −

p = 0
n := size of A
for initialize i := 0, when i < n, update (increase i by 1), do:
if A[i] is same as 1, then:
(increase p by 1)
print p
for initialize i := 1, when i < n, update (increase i by 1), do:
if A[i] is same as 1, then:
print A[i - 1]
print A[n - 1]

## Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;

void solve(vector<int> A) {
int i, p = 0;
int n = A.size();
for (i = 0; i < n; i++) {
if (A[i] == 1)
p++;
}
cout << p << endl;
for (i = 1; i < n; i++) {
if (A[i] == 1)
cout << A[i - 1] << ", ";
}
cout << A[n - 1];
}
int main() {
vector<int> A = { 1, 2, 3, 1, 2, 3, 4, 5 };
solve(A);
}

## Input

{ 1, 2, 3, 1, 2, 3, 4, 5 }

2
3, 5