Motu eats the ice-cream as twice the speed of Patlu (that’s the secret for his health :p ). If, anyone of them is eating the ice-cream, then definitely other one cannot have that ice-cream. However, if both of them reach the same ice-cream, Motu snatches it away from Patlu (he got more power than patlu :( ) and eats it. The winner will be the one who will eat more number of ice-creams.
Since, both of them are too busy in eating ice-creams they forgot to count the number of ice-creams they have already eaten. Can you help them in deciding the winner.
P.S.- Since, ‘WINTER HAS ALREADY ARRIVED’ ,so ice-creams will not melt as it is freezing out there.
Note : Time taken to eat a ice-cream is directly proportional to its height.
Example:
Input: n = 4, a = {2,6,2,1,7}
Output:
4 1 Motu
Approach:
C++
#include <bits/stdc++.h>using namespace std;void motuPatlu(int n, vector<float> &a){int i = 0, j = a.size() - 1, cnt1 = 0, cnt2 = 0;int lasti = -1, lastj = a.size();while (i < j){if (a[i] < 2 * a[j]){a[j] -= a[i] / 2;a[i] = 0;lasti = i, lastj = j;cnt1++;i++;}else if (a[i] > 2 * a[j]){a[i] -= a[j] * 2;a[j] = 0;lasti = i;lastj = j;cnt2++;j--;}else if (a[i] == 2 * a[j]){a[i] = 0;a[j] = 0;lasti = i;lastj = j;cnt1++;cnt2++;i++;j--;}}if (i == j){if (lasti == i && lastj != j)cnt1++;else if (lasti != i && lastj == j)cnt2++;else if (lasti != i && lastj != j){cnt1++;}}cout << cnt1 << " " << cnt2 << "\n";if (cnt1 == cnt2)cout << "Tie\n";else if (cnt1 > cnt2)cout << "Motu\n";elsecout << "Patlu\n";}int main(){int n = 5;vector<float> a = {2, 6, 2, 1, 7};motuPatlu(n, a);return 0;}
No comments:
Post a Comment