- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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++ code to count days to complete reading book
Suppose we have an array A with n elements and have another value t. On ith day Amal spends A[i] seconds in work. In the free time he reads a book. The entire book will take t seconds to complete. We have to find how many days he will need to read the complete book.
So, if the input is like A = [86400, 86398]; t = 2, then the output will be 2, because one day has 86400 seconds, and first day is totally blocked. On second day he will get 2 seconds to complete the book.
Steps
To solve this, we will follow these steps −
cnt := 1 n := size of A for initialize i := 0, when i < n, update (increase i by 1), do: x := A[i] t := t - 86400 - x if t <= 0, then: return cnt (increase cnt by 1)
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; int solve(vector<int> A, int t){ int cnt = 1; int n = A.size(); for (int i = 0; i < n; i++){ int x = A[i]; t -= 86400 - x; if (t <= 0){ return cnt; } ++cnt; } } int main(){ vector<int> A = { 86400, 86398 }; int t = 2; cout << solve(A, t) << endl; }
Input
{ 86400, 86398 }, 2
Output
2
Advertisements