
- 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 final number of characters are there after typing n characters in a crazy writer
Suppose we have an array A with n elements, and another value c is there. There is a crazy word processor present in our system where we can type characters but if we do not type for consecutive c seconds, all of the written letters will be removed. A[i] represents the time when we have typed the ith character. We have to find the final number of characters that will remain on the screen after typing all n characters.
So, if the input is like A = [1, 3, 8, 14, 19, 20]; c = 5, then the output will be 3, because at the second 8 there will be 3 words on the screen. Then, everything disappears at the second 13. At the seconds 14 and 19 another two characters are typed, and finally, at the second 20, one more character is typed, and a total of 3 letters remain on the screen.
Steps
To solve this, we will follow these steps −
s := 1 n := size of A for initialize i := 1, when i < n, update (increase i by 1), do: if (A[i] - A[i - 1]) <= c, then: (increase s by 1) Otherwise s := 1 return s
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int c) { int s = 1; int n = A.size(); for (int i = 1; i < n; i++) { if ((A[i] - A[i - 1]) <= c) { s++; } else { s = 1; } } return s; } int main() { vector<int> A = { 1, 3, 8, 14, 19, 20 }; int c = 5; cout << solve(A, c) << endl; }
Input
{ 1, 3, 8, 14, 19, 20 }, 5
Output
3
- Related Articles
- How to count number of characters in EditText while typing in android?
- How to count number of characters in Text box while typing in iOS?
- Program to get final string after shifting characters with given number of positions in Python
- C program to count characters, lines and number of words in a file
- Python Program to Count Number of Lowercase Characters in a String
- Count Number of Lowercase Characters in a String in Python Program
- Program to count number of characters in each bracket depth in Python
- C++ Program to count number of characters to be removed to get good string
- C Program to delete n characters in a given string
- Program to count number of distinct characters of every substring of a string in Python
- Insert a character after every n characters in JavaScript
- Count number of substrings with exactly k distinct characters in C++
- How to count the number characters in a Java string?
- How to count the number of repeated characters in a Golang String?
- C# program to count upper and lower case characters in a given string
