Given an array of integers arr
, a lucky integer is an integer that has a frequency in the array equal to its value.
Return the largest lucky integer in the array. If there is no lucky integer return -1
.
Example 1:
Input: arr = [2,2,3,4] Output: 2 Explanation: The only lucky number in the array is 2 because frequency[2] == 2.
Example 2:
Input: arr = [1,2,2,3,3,3] Output: 3 Explanation: 1, 2 and 3 are all lucky numbers, return the largest of them.
Example 3:
Input: arr = [2,2,2,3,3] Output: -1 Explanation: There are no lucky numbers in the array.
Constraints:
1 <= arr.length <= 500
1 <= arr[i] <= 500
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 method for finding a lucky integer is to examine each number and count how often it appears in the group. We then check if any number's count matches the number itself.
Here's how the algorithm would work step-by-step:
def find_lucky_integer(numbers):
largest_lucky_integer = -1
for current_number in numbers:
number_count = 0
for number in numbers:
if number == current_number:
number_count += 1
# Check if the number's frequency equals the number itself
if current_number == number_count:
#We only want to consider it lucky if it is
if current_number > largest_lucky_integer:
largest_lucky_integer = current_number
#Return -1 if no lucky number is found
return largest_lucky_integer
The most efficient way to find the lucky number is to first count how many times each number appears in the list. Then, we simply check if any number's count matches the number itself, prioritizing the largest such number.
Here's how the algorithm would work step-by-step:
def find_lucky_integer(integers: list[int]) -> int:
number_counts = {}
for number in integers:
if number in number_counts:
number_counts[number] += 1
else:
number_counts[number] = 1
# Iterate through unique numbers in descending order.
for number in sorted(number_counts.keys(), reverse=True):
# Check if the count matches the number itself
if number_counts[number] == number:
return number
# If no lucky integer is found, return -1.
return -1
Case | How to Handle |
---|---|
Null or empty input array | Return -1 immediately as there are no elements to process. |
Array with a single element | Check if the single element's value is equal to 1, returning the element or -1 respectively. |
Array with all identical values (e.g., [2, 2, 2]) | The frequency will equal the value in this case, so return the value if it's the only one; otherwise, find the largest lucky number. |
Array with no lucky integers | After processing the array, if no lucky integer is found, return -1. |
Array with multiple lucky integers | The algorithm should correctly identify all lucky integers and return the largest among them. |
Large array size (performance considerations) | Use a hash map (dictionary) to efficiently count frequencies instead of nested loops for better time complexity. |
Array containing zero values | The algorithm should correctly handle zero values, which will never be considered lucky since the frequency cannot be zero unless the array is empty. |
Array containing large positive integers close to integer limit | Ensure that the frequency counting and comparison logic does not cause integer overflow issues. |