Given an integer array queries
and a positive integer intLength
, return an array answer
where answer[i]
is either the queries[i]th
smallest positive palindrome of length intLength
or -1
if no such palindrome exists.
A palindrome is a number that reads the same backwards and forwards. Palindromes cannot have leading zeros.
Example 1:
Input: queries = [1,2,3,4,5,90], intLength = 3 Output: [101,111,121,131,141,999] Explanation: The first few palindromes of length 3 are: 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 202, ... The 90th palindrome of length 3 is 999.
Example 2:
Input: queries = [2,4,6], intLength = 4 Output: [1111,1331,1551] Explanation: The first six palindromes of length 4 are: 1001, 1111, 1221, 1331, 1441, and 1551.
Constraints:
1 <= queries.length <= 5 * 104
1 <= queries[i] <= 109
1 <= intLength <= 15
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to finding a palindrome of a specific length involves generating every possible combination of characters for that length. Then, it checks if each generated string is a palindrome. This method is exhaustive, ensuring all possibilities are considered.
Here's how the algorithm would work step-by-step:
def find_palindrome_with_fixed_length_brute_force(required_length, requirement):
smallest_number = 10**(required_length - 1)
largest_number = 10**required_length - 1
for current_number in range(smallest_number, largest_number + 1):
number_as_string = str(current_number)
# Check if the current number is a palindrome.
if number_as_string == number_as_string[::-1]:
# Filter palindromes based on the additional requirement.
if current_number == requirement:
return current_number
# If no palindrome meets the requirements, return -1.
return -1
The most efficient way to find a palindrome with a specific length involves constructing it directly rather than searching. We create the first half and then mirror it to form the palindrome. Adjustments are needed if we're dealing with an odd-length palindrome to handle the middle character correctly.
Here's how the algorithm would work step-by-step:
def find_palindrome_with_fixed_length(queries, int_length):
results = []
for query in queries:
adjusted_number = query - 1
number_as_string = str(adjusted_number)
# Determine the length of the first half.
first_half_length = (int_length + 1) // 2
# If the adjusted number is too small.
minimum_start = 10 ** (first_half_length -1)
maximum_range = 10 ** (first_half_length)
if adjusted_number < minimum_start -1 or adjusted_number >= maximum_range -1 :
results.append(-1)
continue
first_half = number_as_string.zfill(first_half_length)
reversed_first_half = first_half[::-1]
# Adjust the concatenation based on even/odd length.
if int_length % 2 == 0:
palindrome = first_half + reversed_first_half
else:
palindrome = first_half + reversed_first_half[1:]
results.append(int(palindrome))
return results
Case | How to Handle |
---|---|
Null or empty queries array | Return an empty array since there are no queries to process. |
palindromeLength is zero or negative | Return an array filled with -1, indicating no such palindrome exists because length has to be positive. |
palindromeLength is extremely large, causing integer overflow when calculating the range of palindromes | Use a data type like 'long' to handle large palindrome lengths and check for overflows during palindrome generation. |
Queries contain very large numbers that exceed the total number of possible palindromes for the given palindromeLength | Return -1 for queries that exceed the maximum possible palindrome index. |
palindromeLength is even | The solution should correctly construct palindromes with an even number of digits, mirroring the first half. |
palindromeLength is odd | The solution should correctly construct palindromes with an odd number of digits, including a central digit. |
queries array contains duplicate query values | Process each query independently; duplicate queries simply result in the same palindrome being computed multiple times. |
The smallest possible palindrome exceeds the maximum integer value | Return -1 for all queries as no valid integer palindrome can be generated in the current environment. |