Magnets
Practice this coding question on Coding Gurukul.
Problem statement
A science club places bar magnets in a row for a photograph. Every magnet is represented by either 10 or 01 according to its orientation. The photographer divides the row into consecutive groups: magnets with the same orientation stay in one group, and a new group begins whenever the orientation changes. The group count helps the club plan labels for the finished display. Your task Given the orientations in their existing order, count the number of maximal consecutive groups of equal strings. You may not reorder magnets. The first magnet always starts a group; every later magnet starts another group only if its orientation differs from the immediately preceding magnet. Important details The same orientation can occur in several different groups when another orientation separates it. An entire row with one orientation forms one group, not zero. Worked example The sequence 10, 10, 01, 01, 10 forms three groups: the first two magnets, the middle two magnets, and the final magnet.
Input format
First line: n. Next n lines: either 10 or 01.
Output format
Print the number of consecutive groups.
Constraints
1 <= n <= 100000.
Sample input
5 10 10 01 01 10
Sample output
3
Sign in to run or submit a solution in C++, Java or Python.