A designer is laying out a single long row of n floor tiles down a runway. Each tile is stamped with one of exactly two styles, recorded as 0 or 1. A contiguous stretch of the runway (a subarray of consecutive tiles) is called a CLEAN STRETCH if no two neighboring tiles inside that stretch share the same style -- equivalently, the styles strictly alternate across the whole stretch. Every single tile, taken on its own, is trivially a clean stretch. Given the styles of all n tiles in order, count how many contiguous stretches of the runway are clean stretches.
Line 1: an integer n. Line 2: n integers, each 0 or 1, the style of each tile in order, space-separated.
Print a single integer: the number of contiguous clean stretches.
Example 1
Input
4 0 1 1 1
Expected
5
Explanation
The runway styles are 0, 1, 1, 1. Every single tile is a clean stretch on its own (4 of them). The two-tile stretch covering positions 1-2 (styles 0,1) alternates and is clean. Every stretch containing positions 2-3 or 3-4 (styles 1,1) is not clean because it repeats a style, and no stretch of length 3 or 4 avoids that repeat. In total there are 4 + 1 = 5 clean stretches.
Example 2
Input
4 1 0 1 0
Expected
10
Explanation
Every adjacent pair of tiles has a different style, so every one of the 10 possible contiguous stretches of this 4-tile runway (4 of length 1, 3 of length 2, 2 of length 3, 1 of length 4) is a clean stretch, giving a total of 10.
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 →