Bricks Game

Patlu and Motu work in building construction, they have to put some number of bricks N from one place to another and started doing their work. They decided, they end up with a fun challenge who will put the last brick.

They follow a simple rule, In the i'th round, Patlu puts i bricks whereas Motu puts ix2 bricks.

There are only N bricks, you need to help find the challenge result to find who put the last brick.

Example:

Input:  n = 13
Output: Motu

Approach

C++

#include <bits/stdc++.h>
using namespace std;

void bricksGame(int n)
{
    int i = 1;
    int flag;
    while (n)
    {
        n = n - i;
        if (n <= 0)
        {

            flag = 1;
            break;
        }
        n = n - 2 * i;
        if (n <= 0)
        {
            flag = 0;
            break;
        }
        i++;
    }
    if (flag == 0)
        cout << "Motu\n";
    else
        cout << "Patlu\n";
}
int main()
{
    int n = 13;

    bricksGame(n);

    return 0;
}


No comments:

Post a Comment