Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time, return that integer.
Example 1:
Input: arr = [1,2,2,6,6,6,6,7,10] Output: 6
Example 2:
Input: arr = [1,1] Output: 1
Constraints:
1 <= arr.length <= 1040 <= arr[i] <= 105When 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 an element that appears more than 25% of the time in a sorted list is straightforward. We check the count of every unique number in the list to see if any of them meet the required frequency.
Here's how the algorithm would work step-by-step:
def find_element_appearing_more_than_twenty_five_percent(numbers):
list_length = len(numbers)
for index in range(list_length):
current_number = numbers[index]
number_count = 0
# Iterate to count occurrences of the current number.
for inner_index in range(list_length):
if numbers[inner_index] == current_number:
number_count += 1
# Check if current number appears more than 25% of the time.
if number_count > list_length / 4:
return current_number
# No element appears more than 25% of the time.
return -1The problem asks us to find the element that appears more than 25% of the time in a sorted list. Instead of checking every element, the key is to smartly pick a few elements to check based on how frequently an element must appear to satisfy the condition.
Here's how the algorithm would work step-by-step:
def findSpecialInteger(array_of_numbers):
array_length = len(array_of_numbers)
# These indices represent approximately 25%, 50%, and 75% of the array.
quarter_index = array_length // 4
potential_elements = [
array_of_numbers[quarter_index],
array_of_numbers[2 * quarter_index],
array_of_numbers[3 * quarter_index],
]
for potential_element in potential_elements:
# Count the occurrences of potential_element.
element_count = array_of_numbers.count(potential_element)
# If the count is greater than 25% of array length, return.
if element_count > array_length / 4:
return potential_element| Case | How to Handle |
|---|---|
| Empty input array | Return -1 since no element can appear more than 25% of the time in an empty array. |
| Array with only one element | Return that single element since it appears 100% of the time, which is > 25%. |
| Array where all elements are the same | Return any element in the array, as it appears 100% of the time. |
| Array where the element appearing > 25% is at the very beginning | The linear search should find it quickly in this scenario. |
| Array where the element appearing > 25% is at the very end | The linear search must iterate to the end to confirm the frequency is > 25%. |
| Integer overflow when calculating frequency or array size | Use appropriate data types (e.g., long) for calculations to avoid overflow, or check the array length against `Integer.MAX_VALUE`. |
| No element appears more than 25% of the time | Return -1 to indicate no such element exists after iterating the entire array. |
| Large input array size | The time complexity should remain O(n) and space complexity O(1) (or O(log n) for binary search solution) to efficiently handle large inputs. |