Best Matched Pair
InterviewTime limit2sMemory limit512 MB
Given up to 1000 distinct integers, find the largest product of two whose product's decimal digits form a consecutive increasing run like 123; print -1 if none exists.
- Level
Medium4 of 10
- Topics
- Implementation, Brute force, Math, Sorting
- Solved
- No attempts yet
Problem
You work as an engineer at a game company in Tokyo. The company holds an event for its whole staff every summer. This year the event takes place in Tokyo, and you join it on the organizing side. Your assignment is to plan a recreation game that every participant plays at the same time.
After working through several ideas, you settled on the rules below.
- Before the game starts, each player receives one positive integer.
- Each player forms a pair with another player, and the pairs compete by comparing the products of their two integers.
- A player may change partners any number of times before the game ends, but cannot have two or more partners at the same time.
- When the game ends, the pair with the largest product wins.
The integers that were handed out must satisfy one more condition for a pair to form.
Reading the product of the two integers as a string, the digits must increase by 1 and run consecutively from left to right. For example, 2, 23, and 56789 satisfy the condition, while 21, 334, 135, and 89012 do not.
With these rules, several winning pairs with the same product can appear depending on the situation. Even so, once a set of integers is given, the largest product of two integers can be computed.
Given the set of distinct integers to hand out to the players, compute the largest product of two integers that satisfies the rules above.
Input
The input consists of a single test case in the following format.
N
a1 a2 ... aN
The first line contains a positive integer , the number of players. is an integer between and . The second line contains positive integers, the numbers given to the players. For there is one space between and . For every , , and if then .
Output
Print the largest product of two integers that satisfies the pairing condition. If no two players can form a pair, print .