
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Program to count number of characters in each bracket depth in Python
- C++ program to count number of steps needed to make sum and the product different from zero
- Program to count number of similar substrings for each query in Python
- Golang Program to Count the Number of Digits in a Number
- Program to count number of ways ball can drop to lowest level by avoiding blacklisted steps in Python
- Program to find number of optimal steps needed to reach destination by baby and giant steps in Python
- Program to find number of steps to solve 8-puzzle in python
- Count number of rows in each table in MySQL?
- Program to count number of palindromic substrings in Python
- Program to count number of unhappy friends in Python
- Program to count number of homogenous substrings in Python
- Program to count number of nice subarrays in Python
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- 8085 program to count number of once in the given 8-bit number
- Program to find number of minimum steps to reach last index in Python

Advertisements