
- 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
Find time taken for signal to reach all positions in a string in C++
In this tutorial, we will be discussing a program to find time taken for signal to reach all positions in a string
For this we will be provided with a string containing ‘x’ and ‘o’. A signal originates from ‘x’ and travels in both directions changing one ‘o’ value in one unit time. Our task is to calculate the complete time to convert whole string into ‘x’s.
Example
#include <bits/stdc++.h> using namespace std; //calculating the total required time int findMaximumDuration(string s, int n) { int right = 0, left = 0; int count = 0, maximumLength = INT_MIN; s = s + '1'; for (int i = 0; i <= n; i++) { if (s[i] == 'o') count++; else { if (count > maximumLength) { right = 0; left = 0; if (s[i] == 'x') right = 1; if (((i - count) > 0) && (s[i - count - 1] == 'x')) left = 1; count = ceil((double)count / (right + left)); maximumLength = max(maximumLength, count); } count = 0; } } return maximumLength; } int main() { string str = "xooxoooxxoooxoooxooxooox"; int length = str.size(); cout << findMaximumDuration(str, length); return 0; }
Output
2
- Related Questions & Answers
- Find time taken for signal to reach all positions in a string - C++
- How to measure time taken by a function in C?
- Find numbers of balancing positions in string in C++
- How to obtain the time taken for a method to be executed in TestNG?
- Representation of a Discrete Time Signal
- Calculating time taken to type words in JavaScript
- Find All Anagrams in a String in C++
- C# Program to find all substrings in a string
- Continuous-Time Vs Discrete-Time Sinusoidal Signal
- How to measure the time taken by a function in Java?
- Problem: Time taken by tomatoes to rot in JavaScript
- Find all substrings in a string using C#
- C++ code to find the area taken by boxes in a container
- Energy of a Power Signal over Infinite Time
- Time taken by savepoint to perform backup in SAP HANA
Advertisements