FInd the minimum number of switches to turn on all bulbs
<p>I am trying to understand the problem given <a href="http://qa.geeksforgeeks.org/4118/find-the-minimum-number-switches-you-have-press-turn-all-bulbs" rel="noreferrer">here</a> and its solution:</p>

<p>The problem states:</p>

<p>N light bulbs are connected by a wire. Each bulb has a switch associated with it, however due to faulty wiring, a switch also changes the state of all the bulbs to the right of current bulb. Given an initial state of all bulbs, find the minimum number of switches you have to press to turn on all the bulbs. You can press the same switch multiple times.</p>

<p>Note : 0 represents the bulb is off and 1 represents the bulb is on.</p>

<pre><code>Example:

Input : [0 1 0 1]
Return : 4

Explanation :

press switch 0 : [1 0 1 0]
press switch 1 : [1 1 0 1]
press switch 2 : [1 1 1 0]
press switch 3 : [1 1 1 1]
</code></pre>

<p>One of the answers given is:</p>

<pre><code>int solve(int A[], int N) {

    int state= 0, ans = 0;
    for (int i = 0; i &lt; N;i++) {
        if (A[i] == state) {
            ans++;
            state = 1 - state;
        }
    }

    return ans;
}
</code></pre>

<p>I can't seem to wrap my head around how the if statement does the correct thing. </p>