Mock Bronze B2 · "Cow Bingo"

← Back to mock contests · Original problem · Suggested time: 60–90 minutes

Problem

Farmer John has N cows arranged in a row, each labeled with a distinct positive integer ai (1 ≤ ai ≤ 20). He wants to give each cow a "bingo card" that is a subset of {1, …, 20}. A cow's bingo card wins if the multiset of labels of its row neighbours (the cow's immediate left and right neighbour, where they exist) is exactly equal to the cow's card.

Given the row of labels, find the number of cows whose bingo card matches its actual two neighbours' labels. More precisely: each cow is given the bingo card consisting of the labels {ai−1, ai+1} where positions outside the row are skipped. Count how many cows would mark a "bingo" if their card is exactly the set of values appearing as neighbours.

(This is a deliberately ambiguous Bronze-style statement — part of the exercise is pinning down exactly what's being asked before you code.)

Constraints

Sample

Input
5
3 7 3 5 7

Output
3

(Sample is illustrative — make your own samples to test edge cases before submitting.)

Skills this targets

Self-grade after attempting

Solution sketch (open after your timer is up)
  • For each cow at position i, compute the multiset of its existing neighbours: 1-element if endpoint, 2-element otherwise.
  • The cow's "card" is the set of distinct values among those neighbours. Compare equality directly.
  • With N ≤ 1000 a straightforward O(N) sweep is well within limits — no clever DS needed.
  • Edge case: cow at position 0 has only one neighbour; the card has 1 element. Be careful with set vs. multiset comparison if duplicate neighbour values are possible (here all labels are distinct, but you should re-read the statement to confirm).