C++ code to find how long TV are on to watch a match


Suppose we have an array A with n elements. Amal wants to watch a match of 90 minutes and there is no break. Each minute can be either interesting or boring. If 15 consecutive minutes are boring then Amal immediately turns off the TV. There will be n interesting minutes represented by the array A. We have to calculate for how many minutes Amal will watch the game.

So, if the input is like A = [7, 20, 88], then the output will be 35, because after 20, he will still watch the game till 35, then turn off it.

Steps

To solve this, we will follow these steps −

Define an array a of size: 100.
n := size of A
for initialize i := 1, when i <= n, update (increase i by 1), do:
   a[i] := A[i - 1]
   if a[i] - a[i - 1] > 15, then:
      Come out from the loop
return minimum of (a[i - 1] + 15) and 90

Example

Let us see the following implementation to get better understanding −

#include <bits/stdc++.h>
using namespace std;
int solve(vector<int> A){
   int i, a[100];
   int n = A.size();
   for (i = 1; i <= n; i++){
      a[i] = A[i - 1];
      if (a[i] - a[i - 1] > 15)
         break;
   }
   return min(a[i - 1] + 15, 90);
}
int main(){
   vector<int> A = { 7, 20, 88 };
   cout << solve(A) << endl;
}

Input

{ 7, 20, 88 }

Output

35

Updated on: 30-Mar-2022

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements