A control room monitors n beacon lights arranged in a perfect circle, each currently glowing one of two colors encoded as 0 or 1. A beacon is flagged as a pivot beacon when both of its immediate neighbors around the ring -- the one just before it and the one just after it -- glow a color different from the beacon itself. Because the beacons form a closed ring, the beacon before index 0 is the beacon at index n-1, and the beacon after index n-1 is the beacon at index 0. Count how many of the n beacons are pivot beacons.
Line 1: an integer n -- the number of beacons. Line 2: a string of exactly n characters, each either '0' or '1', giving the color of each beacon in ring order (index 0 through n-1).
A single integer: the number of pivot beacons.
Example 1
Input
4 0101
Expected
4
Explanation
Every beacon's two ring-neighbors differ from it: beacon 0 ('0') has left neighbor beacon 3 ('1') and right neighbor beacon 1 ('1'), both different; the same alternation holds for beacons 1, 2, and 3 by symmetry, so all 4 beacons are pivots, giving 4.
Example 2
Input
3 111
Expected
0
Explanation
All three beacons glow the same color '1', so no beacon has even one neighbor of a different color, let alone two. No beacon qualifies as a pivot, so the count is 0.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →