ZipRecruiterOA #6
Cycle Shift
EasyJudge-ready
Given an array of positive integers, count index pairs i<j where the two numbers have the same number of decimal digits and one can become the other through any number of cyclic shifts (including shifting by 0). Repeated values are counted separately by index. The fixed source example's answer is corrected from 3 to 5; the basis for the erratum is in the notes.
Input
The first line contains n (site range 1≤n≤100), and the second line contains n positive integers a[i] (source range 1≤a[i]≤10000).
Output
Output the number of qualifying index pairs.
Example 1
Input:
9
13 5604 31 2 13 4560 546 654 456
Output:
5
Example 2
Input:
4
11 11 11 11
Output:
6
Example 3
Input:
4
123 231 312 132
Output:
3
Example notes
Group numbers by the minimum over all cyclic shifts of their decimal strings; each group contributes C(frequency,2). The public example's output is corrected to 5 based on the five matching pairs listed in the source explanation.
Editorial
Loading solution…