Motu and Patlu

Motu and Patlu are very good friends and they love to eat ice-creams. Once they thought of playing a game, so they bought “n” ice-creams from the market of varying heights (may be same). They arranged the ice-creams in a line in random order. Motu starts to eat ice-creams from left to right and Patlu from right to left. The heights of the ice-creams are known.

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 nvector<float&a)
{
    int i = 0j = a.size() - 1cnt1 = 0cnt2 = 0;
    int lasti = -1lastj = a.size();
    while (i < j)
    {
        if (a[i] < 2 * a[j])
        {

            a[j] -= a[i] / 2;
            a[i] = 0;
            lasti = ilastj = 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";
    else
        cout << "Patlu\n";
}
int main()
{

    int n = 5;
    vector<floata = {26217};

    motuPatlu(na);

    return 0;
}


No comments:

Post a Comment