Add Modulo 10 (Codeforces 1714E)
Tags: Math, number theory

You are given an array of $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$You can apply the following operation an arbitrary number of times:   select an index $$$i$$$ ($$$1 \le i \le n$$$) and replace the value of the element $$$a_i$$$ with the value $$$a_i + (a_i \bmod 10)$$$, where $$$a_i \bmod 10$$$ is the remainder of the integer dividing $$$a_i$$$ by $$$10$$$. For a single index (value $$$i$$$), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of $$$a_i$$$ is taken into account each time. For example, if $$$a_i=47$$$ then after the first operation we get $$$a_i=47+7=54$$$, and after the second operation we get $$$a_i=54+4=58$$$.Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.For example, you have an array $$$[6, 11]$$$.   Let's apply this operation to the first element of the array. Let's replace $$$a_1 = 6$$$ with $$$a_1 + (a_1 \bmod 10) = 6 + (6 \bmod 10) = 6 + 6 = 12$$$. We get the array $$$[12, 11]$$$.  Then apply this operation to the second element of the array. Let's replace $$$a_2 = 11$$$ with $$$a_2 + (a_2 \bmod 10) = 11 + (11 \bmod 10) = 11 + 1 = 12$$$. We get the array $$$[12, 12]$$$. Thus, by applying $$$2$$$ operations, you can make all elements of an array equal.

Input: The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. What follows is a description of each test case.The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array.The second line of each test case contains $$$n$$$ integers $$$a_i$$$ ($$$0 \le a_i \le 10^9$$$) — array elements.It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.

Output: For each test case print:  YES if it is possible to make all array elements equal;  NO otherwise. You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) .

Note: The first test case is clarified above.In the second test case, it is impossible to make all array elements equal.In the third test case, you need to apply this operation once to all elements equal to $$$5$$$.In the fourth test case, you need to apply this operation to all elements until they become equal to $$$8$$$.In the fifth test case, it is impossible to make all array elements equal.In the sixth test case, you need to apply this operation to all elements until they become equal to $$$102$$$.

Sample Inputs:
10
2
6 11
3
2 18 22
5
5 10 5 10 5
4
1 2 4 8
2
4 5
3
93 96 102
2
40 6
2
50 30
2
22 44
2
1 5

Sample Outputs:
Yes
No
Yes
Yes
No
Yes
No
No
Yes
No

Solution (Python):
def main():
    reps = int(input())
    for iteration in range(reps):
        n = int(input())
        arr = [int(num.strip()) for num in input().split()]
        group = set()
        for num in arr:
            digit = num % 10
            if digit == 0:
                group.add(num)
            elif digit == 5:
                group.add(num + 5)
            elif digit in [1, 2, 4, 8]:
                if num // 10 % 2 == 0:
                    group.add(1)
                else:
                    group.add(2)
            elif num // 10 % 2 == 0:
                group.add(2)
            else:
                group.add(1)
        print('YES' if len(group) == 1 else 'NO')
if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n = int(input())
    arr = list(map(int, input().split()))
    a = arr[0]
    g1 = False
    g2 = False
    g5 = False
    g1n = set([1, 2, 4, 8])
    g2n = set([3, 6, 7, 9])
    mx = -1
    if a % 5 == 0:
        g5 = True
        mx = a + a % 10
    elif a < 10:
        if a in g1n:
            g1 = True
        else:
            g2 = True
    elif a % 10 in g1n:
        a = a // 10
        if a % 10 % 2:
            g2 = True
        else:
            g1 = True
    else:
        a = a // 10
        if a % 10 % 2:
            g1 = True
        else:
            g2 = True
    ori = -1
    if g1:
        ori = 1
    elif g2:
        ori = 2
    else:
        ori = 5
    for a in arr:
        g1 = False
        g2 = False
        g5 = False
        if a % 5 == 0:
            curr = a + a % 10
            if curr != mx:
                return 'No'
            g5 = True
        elif a < 10:
            if a in g1n:
                g1 = True
            else:
                g2 = True
        elif a % 10 in g1n:
            a = a // 10
            if a % 10 % 2:
                g2 = True
            else:
                g1 = True
        else:
            a = a // 10
            if a % 10 % 2:
                g1 = True
            else:
                g2 = True
        if g1 and ori == 1:
            continue
        if g2 and ori == 2:
            continue
        if g5 and ori == 5:
            continue
        return 'No'
    return 'Yes'
for _ in range(int(input())):
    print(main())

Solution (Python):
from sys import stdin, stdout
import math

def main():
    tests = int(stdin.readline())

    def Modulo(nums, numLen):
        if numLen == 1:
            return 'Yes'
        fives = 0
        lowSix = 0
        lowTFE = 0
        for num in nums:
            temp = num % 20
            if temp % 5 == 0:
                fives = 1
            elif temp in [1, 2, 4, 8, 13, 16, 17, 19]:
                lowTFE = 1
            elif temp in [3, 6, 7, 9, 11, 12, 14, 18]:
                lowSix = 1
        if fives + lowSix + lowTFE > 1:
            return 'No'
        if fives == 1:
            minNum = min(nums)
            maxNum = max(nums)
            if minNum == maxNum:
                return 'Yes'
            if minNum % 10 == 5 and maxNum - minNum == 5:
                return 'Yes'
            if minNum % 10 == 0 and maxNum - minNum == 0:
                return 'Yes'
            else:
                return 'No'
        return 'Yes'
    for i in range(tests):
        numLen = int(stdin.readline())
        nums = [int(x) for x in stdin.readline().split()]
        stdout.write(str(Modulo(nums, numLen)) + '\n')
if __name__ == '__main__':
    main()