Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Best Matched Pair

You are working for a worldwide game company as an engineer in Tokyo. This company holds an annual event for all the staff members of the company every summer. This year's event will take place in Tokyo. You will participate in the event on the side of the organizing staff. And you have been assigned to plan a recreation game which all the participants will play at the same time.

After you had thought out various ideas, you designed the rules of the game as below.

 Each player is given a positive integer before the start of the game.

 Each player attempts to make a pair with another player in this game, and formed pairs compete with each other by comparing the products of two integers.

 Each player can change the partner any number of times before the end of the game, but cannot have two or more partners at the same time.

 At the end of the game, the pair with the largest product wins the game.

In addition, regarding the given integers, the next condition must be satisfied for making a pair.

 The sequence of digits obtained by considering the product of the two integers of a pair as a string must be increasing and consecutive from left to right. For example, 2, 23, and 56789 meet this condition, but 21, 334, 135 or 89012 do not.

Setting the rules as above, you noticed that multiple pairs may be the winners who have the same product depending on the situation. However, you can find out what is the largest product of two integers when a set of integers is given.

Your task is, given a set of distinct integers which will be assigned to the players, to compute the largest possible product of two integers, satisfying the rules of the game mentioned above.

Input

The input consists of a single test case formatted as follows.

$N$

$a_1$ $a_2$ ... $a_N$

The first line contains a positive integer $N$ which indicates the number of the players of the game. $N$ is an integer between 1 and 1,000. The second line has $N$ positive integers that indicate the numbers given to the players. For $i = 1, 2, ... , N - 1$, there is a space between $a_i$ and $a_{i+1}$. $a_i$ is between 1 and 10,000 for $i = 1, 2, ..., N$, and if $i \ne j$, then $a_i \ne a_j$.

Output

Print the largest possible product of the two integers satisfying the conditions for making a pair. If any two players cannot make a pair, print -1.

Sample Input 1

2
1 2

Output for the Sample Input 1

2

Sample Input 2

3
3 22 115

Output for the Sample Input 2

345

Sample Input 3

2
1 11

Output for the Sample Input 3

-1

Sample Input 4

2
5 27

Output for the Sample Input 4

-1

Sample Input 5

2
17 53

Output for the Sample Input 5

-1

Sample Input 6

10
53 43 36 96 99 2 27 86 93 23

Output for the Sample Input 6

3456



Sample input:
2
1 2


Sample output:
2

Submission status: Accepted

Solution code:
import itertools


def check(x: int) -> bool:
    s = str(x)
    prev = ord(s[0])
    for c in s[1:]:
        if ord(c) != prev+1:
            return False
        prev = ord(c)
    return True


def main() -> None:
    n = int(input())
    v = list(map(int, input().split(' ')))
    max_ = -1
    for c in itertools.combinations(sorted(v), 2):
        p = c[0] * c[1]
        if check(p):
            max_ = max(max_, p)

    print(max_)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
I=input;n=int(I())
def f(x):
    a=x%10;x//=10
    while x:
        if x%10+1!=a:return 0
        a=x%10;x//=10
    return 1

a=list(map(int,I().split()))
b=-1
for i in range(n):
    for j in a[i+1:]:
        c=a[i]*j
        if(f(c) and b<c):b=c
print(b)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
ans = -1
for i in range(N):
    for j in range(i + 1, N):
        num = [int(x) for x in str(A[i] * A[j])]
        if all(num[i] + 1 == num[i + 1] for i in range(len(num) - 1)) and ans < A[i] * A[j]:
            ans = A[i] * A[j]
print(ans)

Submission status: Accepted

Solution code:
def is_increasing(n):
    for c1,c2 in zip(str(n),str(n)[1:]):
        if int(c1) + 1 != int(c2):
            return False
    return True

N = int(input())
src = list(map(int,input().split()))
ans = -1
for i in range(N-1):
    for j in range(i+1,N):
        if is_increasing(src[i] * src[j]):
            ans = max(ans, src[i] * src[j])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

safe = list()
for i in range(n - 1):
    for j in range(i + 1, n):
        pro = a[i] * a[j]

        digit = [pro % (10 ** k) // (10 ** (k - 1)) for k in range(1, 10)]

        while digit[-1] == 0:
            digit = digit[:-1]

        if len(digit) == 1:
            safe.append(pro)
            continue

        flag = True
        for k in range(len(digit) - 1):
            if digit[k + 1] - digit[k] != -1:
                flag = False
                break

        if flag:
            safe.append(pro)

if safe:
    safe.sort()
    print(safe[-1])
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

safe = set()
for i in range(n - 1):
    for j in range(i + 1, n):
        pro = a[i] * a[j]

        digit = [pro % (10 ** k) // (10 ** (k - 1)) for k in range(1, 10)]

        while digit[-1] == 0:
            digit = digit[:-1]

        if len(digit) == 1:
            safe.add(pro)
            continue

        flag = True
        for k in range(len(digit) - 1):
            if digit[k + 1] - digit[k] != -1:
                flag = False
                break

        if flag:
            safe.add(pro)

if safe:
    print(max(safe))
else:
    print(-1)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

ma = -1
for i in range(n - 1):
    for j in range(i + 1, n):
        pro = a[i] * a[j]

        digit = [pro % (10 ** k) // (10 ** (k - 1)) for k in range(1, 10)]

        while digit[-1] == 0:
            digit = digit[:-1]

        if len(digit) == 1:
            ma = max(ma, pro)
            continue

        flag = True
        for k in range(len(digit) - 1):
            if digit[k + 1] - digit[k] != -1:
                flag = False
                break

        if flag:
            ma = max(ma, pro)

print(ma)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))

ma = -1
for i in range(n - 1):
    for j in range(i + 1, n):
        pro = a[i] * a[j]
        
        pre = pro % 10
        pro //= 10

        while pro:
            if pre - pro % 10 == 1:
                pre = pro % 10
                pro //= 10
            else:
                break

        if not pro:
            ma = max(ma, a[i] * a[j])
print(ma)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n = I()
        a = LI()
        r = -1
        for i in range(n):
            b = a[i]
            for c in a[i+1:]:
                t = b*c
                if r >= t:
                    continue
                s = str(t)
                if all([int(s[k]) == int(s[k+1])-1 for k in range(len(s)-1)]):
                    r = t

        rr.append(r)
        break

    return '\n'.join(map(str, rr))


print(main())