A cyclic shift is the operation of rearranging the digits in a number (in decimal format) by moving some digits at the end of the number to before the beginning of the number, while shifting all other digits to the next position. Given two integers of the same length a and b, a would be a cyclic pair of b if it is possible for a to become equal to b after performing cyclic shifts on a, moving 0 or more ending digits to the beginning while shifting all other digits to the next position in the same order.
Given an array of positive integers a, your task is to count the number of cyclic pairs i and j (where 0 <= i < j < a.length), such that a[i] and a[j] have the same number of digits and a[i] is equal to a cyclic shift of a[j].
a = [13, 5604, 31, 2, 13, 4560, 546, 654, 456] return = 5
There are 5 cyclic pairs of numbers - pairs which are equal to each other after cyclic shifts:
a[0] = 13anda[2] = 31(i = 0andj = 2).a[0] = 13anda[4] = 13(i = 0andj = 4).a[2] = 31anda[4] = 13(i = 2andj = 4).a[1] = 5604anda[5] = 4560(i = 1andj = 5).a[6] = 546anda[7] = 654(i = 6andj = 7).
Note that a[6] = 546 and a[8] = 456 are not cyclic pairs - 546 can only be paired with cyclic shift of 546, 465 and 654.
Also, note that a[5] = 4560 and a[8] = 456 are not cyclic pairs because they have different number of digits.
- Count Skipped Numbers After SubtractionsOA · Seen Jun 2026
- Event ID Check Completion TimesOA · Seen Jun 2026
- Check Even-Position MonotonicityOA · Seen Jun 2026
- Count Alternating Tile GroupsOA · Seen Jun 2026
- Count Even-Digit NumbersOA · Seen Jun 2026
- Inventory Discount TrackerOA · Seen Jun 2026
- Construct WDL StringOA · Seen Jun 2026
- Find Sum PairsOA · Seen Jun 2026
public int solution(int[] a) {
// write your code here
}