You are given an unsorted array of integers, nums.
A number x is binary searchable if, after sorting the subarray of nums that contains only x and its surrounding elements, the position of x in the sorted subarray is the same as its position in the original array nums.
More formally, a number nums[i] is binary searchable if:
subarray = sorted([nums[j] for j in range(max(0, i - k), min(len(nums), i + k + 1))]) for some k >= 0.subarray.index(nums[i]) == i - max(0, i - k).Return the number of binary searchable elements in nums.
Example 1:
Input: nums = [1,5,4,3,2] Output: 0 Explanation: No element is binary searchable.
Example 2:
Input: nums = [1,4,2,3] Output: 1 Explanation: Only 4 is binary searchable: - Consider the subarray containing 4 and its surrounding elements: [1,4,2]. - If we sort it: [1,2,4]. - The index of 4 in both arrays is 1.
Example 3:
Input: nums = [1,2,3,4,5] Output: 5 Explanation: Every element is binary searchable.
Constraints:
1 <= nums.length <= 1051 <= nums[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:
To find numbers that could be found using a perfect guessing game strategy, we'll just look at each number in the collection, one at a time. For each number, we'll check if it satisfies the conditions to be found this way, and keep a count of those that do.
Here's how the algorithm would work step-by-step:
def find_searchable_numbers(number_collection):
searchable_number_count = 0
for current_index in range(len(number_collection)):
is_searchable = True
current_number = number_collection[current_index]
# Check if all numbers to the left are smaller or equal
for left_index in range(current_index):
if number_collection[left_index] > current_number:
is_searchable = False
break
if not is_searchable:
continue
# Check if all numbers to the right are bigger or equal
for right_index in range(current_index + 1, len(number_collection)):
if number_collection[right_index] < current_number:
is_searchable = False
break
if is_searchable:
# Increment counter if it meets the criteria
searchable_number_count += 1
return searchable_number_countThe key is to realize a number is 'binary searchable' if and only if all numbers to its left are smaller, and all numbers to its right are larger. We can efficiently determine these conditions by pre-computing running maximums from the left and running minimums from the right, then checking each number.
Here's how the algorithm would work step-by-step:
def find_binary_searchable_numbers(number_list):
list_length = len(number_list)
if list_length == 0:
return 0
left_running_maximums = [0] * list_length
right_running_minimums = [0] * list_length
# Need maximum from the left to compare
left_running_maximums[0] = number_list[0]
for i in range(1, list_length):
left_running_maximums[i] = max(left_running_maximums[i - 1], number_list[i])
# Need minimum from the right to compare
right_running_minimums[list_length - 1] = number_list[list_length - 1]
for i in range(list_length - 2, -1, -1):
right_running_minimums[i] = min(right_running_minimums[i + 1], number_list[i])
binary_searchable_count = 0
for i in range(list_length):
# Check if number is binary searchable
if number_list[i] >= left_running_maximums[i] and number_list[i] <= right_running_minimums[i]:
binary_searchable_count += 1
return binary_searchable_count| Case | How to Handle |
|---|---|
| Null or empty input array | Return an empty list immediately to avoid null pointer exceptions or invalid operations. |
| Array with a single element | Return an empty list as a single element cannot be 'binary searchable'. |
| Array sorted in descending order | The binary search logic must account for this reversal, potentially by reversing the comparison direction. |
| Array with all identical elements | The solution should correctly identify that only the first element is binary searchable. |
| Array with very large numbers (potential for integer overflow) | Use appropriate data types (e.g., long) or validate intermediate calculations to prevent overflow. |
| Presence of duplicate binary searchable numbers | Return all occurrences of valid binary searchable numbers, or specify that only unique instances are required. |
| Very large array (performance considerations) | Ensure the algorithm has a time complexity no worse than O(n log n) to avoid timeouts. |
| Array containing negative numbers and/or zero | The solution should handle negative numbers and zero correctly, without any special treatment needed if using standard comparison operators. |