# 31. Push Dominoes

There are `N` dominoes in a line, and we place each domino vertically upright.

In the beginning, we simultaneously push some of the dominoes either to the left or to the right.

![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/05/18/domino.png)

After each second, each domino that is falling to the left pushes the adjacent domino on the left.

Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.

When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.

For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.

Given a string "S" representing the initial state. `S[i] = 'L'`, if the i-th domino has been pushed to the left; `S[i] = 'R'`, if the i-th domino has been pushed to the right; `S[i] = '.'`, if the `i`-th domino has not been pushed.

Return a string representing the final state.&#x20;

**Example 1:**

```
Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
```

**Example 2:**

```
Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
```

## Solution: (Two Pointer)

**Approach:**\
**For each '.' finding if there is any 'R' on left side and 'L' on right side**\
**Choose the smaller one and decide the position**

```cpp
class Solution
{
public:
    string pushDominoes(string dominoes)
    {

        int n = dominoes.size();
        if (n == 0)
        {
            return dominoes;
        }
        vector<int> leftSide(n, 0);
        vector<int> rightSide(n, 0);

        int rightPos = n + 1;
        for (int i = 0; i < n; i++)
        {

            if (dominoes[i] == 'R')
            {
                rightPos = i;
            }
            else if (dominoes[i] == 'L')
            {
                rightPos = n + 1;
            }

            leftSide[i] = (rightPos == n + 1) ? n + 1 : i - rightPos;
        }

        int leftPos = -1;
        for (int i = n - 1; i >= 0; i--)
        {

            if (dominoes[i] == 'L')
            {
                leftPos = i;
            }
            else if (dominoes[i] == 'R')
            {
                leftPos = -1;
            }

            rightSide[i] = (leftPos == -1) ? n + 1 : leftPos - i;
        }

        for (int i = 0; i < n; i++)
        {
            if (rightSide[i] < leftSide[i])
            {
                dominoes[i] = 'L';
            }
            else if (leftSide[i] < rightSide[i])
            {
                dominoes[i] = 'R';
            }
        }

        return dominoes;
    }
};
```

**Time Complexity: O(n)**


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://soumyajit4419.gitbook.io/ds-algo/arrays/32.-push-dominoes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
