Lisa just got a new math workbook. A workbook contains exercise problems, grouped into chapters. Lisa believes a problem to be special if its index (within a chapter) is the same as the page number where it's located. The format of Lisa's book is as follows:
1.There are n chapters in Lisa's workbook, numbered from 1 to n.
2.The ith chapter has arr[i] problems, numbered from 1 to arr[i].
3.Each page can hold up to k problems. Only a chapter's last page of exercises may contain fewer than k problems.
4.Each new chapter starts on a new page, so a page will never contain problems from more than one chapter.
5.The page number indexing starts at 1.
Given the details for Lisa's workbook, can you count its number of special problems?
Example:
Input: n=5,k=3, arr[]={4,2,6,1,10}
Output: 4
Approach
C++
#include <bits/stdc++.h>using namespace std;int main(){int n = 5, k = 3;int arr[n] = {4, 2, 6, 1, 10};int l = 1;int ans = 0, cnt = 0;for (int i = 0; i < n; i++){cnt = 0;for (int j = 1; j <= arr[i]; j++){if (cnt < k){if (j == l)ans++;cnt++;}else{cnt = 1;l++;if (l == j)ans++;}if (j == arr[i])l++;}}cout << ans << "\n";return 0;}
No comments:
Post a Comment