Problem Statement:
Anna plays video games every day. To complete one game, Anna requires 10 minutes. She has N days to play the game. On each ith day, she has A[i] free hours. Find the maximum number of games that she can complete in N days.
Input Format:
The input consists of two lines:
The first line contains a single integer N denoting the number of days.
The second line contains N space-separated integers denoting the number of free hours.
Output Format:
Print the maximum number of games Anna can play in N days.
Example 1:
Input:
3
2 4 5
Output:
66
Explanation:
On the first day, Anna has 2 free hours (120 minutes). She will play 12 games.
On the second day, she has 4 free hours (240 minutes). She will play 24 games.
On the third day, she has 5 free hours (300 minutes). She will play 30 games.
Total = 12 + 24 + 30 = 66 games.
Sample Case 2:
Input:
2
11 12
Output:
138
Basic Math
Array Traversal
We are given Anna's free time in hours, but the game duration is measured in minutes. Think of a situation where you simplify the units first. Since 1 hour is 60 minutes, and 1 game takes 10 minutes, she can play exactly 6 games every hour! We just need to figure out her total free hours and use that magic number.
1. The Magic Multiplier
Instead of converting hours to minutes and then dividing by 10 for every single day, we can just realize that 1 hour = 60 mins / 10 mins = 6 games.
2. Summing it Up
We can loop through the array of days, add up all her free hours, and simply multiply the total sum by 6 at the very end. By summing first and multiplying once, we avoid doing extra math on every single loop iteration! Note: We use a long long for the total hours just in case the constraints are very large and the sum exceeds the standard integer limit.
Time Complexity
Time: O(N) - We only need to loop through the array of days exactly once.
Space: O(1)- We only use a single variable to keep track of the total hours, so no extra memory is needed.
#include <iostream>
#include <vector>
using namespace std;
long long maxGames(int n, vector<int>& a) {
long long hrs = 0;
// Sum up all the free hours
for (int i = 0; i < n; i++) {
hrs += a[i];
}
// 1 hour = 6 games
return hrs * 6;
}
int main() {
// Sample Case 1
vector<int> a1 = {2, 4, 5};
cout << "Case 1: " << maxGames(3, a1) << "\n"; // Output: 66
// Sample Case 2
vector<int> a2 = {11, 12};
cout << "Case 2: " << maxGames(2, a2) << "\n"; // Output: 138
return 0;
}