

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 }
Output
2 3, 5
- Related Questions & Answers
- Program to count number of characters in each bracket depth in Python
- Program to count number of similar substrings for each query in Python
- C++ program to count number of steps needed to make sum and the product different from zero
- Count number of rows in each table in MySQL?
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Golang Program to Count the Number of Digits in a Number
- Program to find number of steps to solve 8-puzzle in python
- Python Pandas - Count the number of rows in each group
- Program to count number of ways ball can drop to lowest level by avoiding blacklisted steps in Python
- Program to find number of minimum steps to reach last index in Python
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Number of Steps to Reduce a Number in Binary Representation to One in C++
- Count number of factors of a number - JavaScript
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
Advertisements