Count Consecutive Occurrences
EasyPractice library · +10 XPdynamic programmingarraysloops
Given an array of N integers, count the total number of pairs of adjacent elements that are equal. For example, if the array is [1, 1, 2, 2, 2, 3], the pairs of equal adjacent elements are (1, 1) and the first two (2, 2) pairs. The count would be 3.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers representing the array elements.
Output
Print a single integer, the total count of equal adjacent pairs.
Constraints
1 <= N <= 1000. Each array element is between -1000 and 1000.
Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests
Example 1
Input
6 1 1 2 2 2 3
Output
3
The pairs are (1,1) at index 0,1 and (2,2) at index 2,3 and (2,2) at index 3,4. Total 3 pairs.
Example 2
Input
5 5 5 5 5 5
Output
4
All adjacent pairs are equal. There are 4 such pairs.
Hints
# Read the input from stdin and print the answer.
import sys
def main():
data = sys.stdin.read().split()
# your code here
main()
Example 1 input
6 1 1 2 2 2 3
Example 2 input
5 5 5 5 5 5