Watch 10 video solutions for Binary Searchable Numbers in an Unsorted Array, a medium level problem involving Array, Binary Search. This walkthrough by Fireship has 672,828 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Consider a function that implements an algorithm similar to Binary Search. The function has two input parameters: sequence is a sequence of integers, and target is an integer value. The purpose of the function is to find if the target exists in the sequence.
The pseudocode of the function is as follows:
func(sequence, target)
while sequence is not empty
randomly choose an element from sequence as the pivot
if pivot = target, return true
else if pivot < target, remove pivot and all elements to its left from the sequence
else, remove pivot and all elements to its right from the sequence
end while
return false
When the sequence is sorted, the function works correctly for all values. When the sequence is not sorted, the function does not work for all values, but may still work for some values.
Given an integer array nums, representing the sequence, that contains unique numbers and may or may not be sorted, return the number of values that are guaranteed to be found using the function, for every possible pivot selection.
Example 1:
Input: nums = [7] Output: 1 Explanation: Searching for value 7 is guaranteed to be found. Since the sequence has only one element, 7 will be chosen as the pivot. Because the pivot equals the target, the function will return true.
Example 2:
Input: nums = [-1,5,2] Output: 1 Explanation: Searching for value -1 is guaranteed to be found. If -1 was chosen as the pivot, the function would return true. If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return true. If 2 was chosen as the pivot, 2 would be removed. In the next loop, the sequence would have -1 and 5. No matter which number was chosen as the next pivot, the function would find -1 and return true. Searching for value 5 is NOT guaranteed to be found. If 2 was chosen as the pivot, -1, 5 and 2 would be removed. The sequence would be empty and the function would return false. Searching for value 2 is NOT guaranteed to be found. If 5 was chosen as the pivot, 5 and 2 would be removed. In the next loop, the sequence would have only -1 and the function would return false. Because only -1 is guaranteed to be found, you should return 1.
Constraints:
1 <= nums.length <= 105-105 <= nums[i] <= 105nums are unique.
Follow-up: If nums has duplicates, would you modify your algorithm? If so, how?
Problem Overview: You receive an unsorted array and need to count how many numbers would still be found correctly if a standard binary search was executed on the array. A number is "binary searchable" only if every element on its left is smaller or equal and every element on its right is greater or equal. In other words, the element already sits exactly where it would appear in the sorted order relative to the rest of the array.
Approach 1: Brute Force Validation (O(n²) time, O(1) space)
Check every element nums[i] and verify two conditions: all elements to the left are <= nums[i] and all elements to the right are >= nums[i]. This requires two scans for each index. The approach directly mirrors the binary search correctness condition but repeatedly scans the array, leading to quadratic time complexity. Useful for understanding the rule that defines a valid binary searchable element.
Approach 2: Prefix Max + Suffix Min (O(n) time, O(n) space)
Precompute two helper arrays. The first array stores the maximum value seen so far from the left (prefixMax[i]). The second stores the minimum value from the right (suffixMin[i]). An element nums[i] is valid if prefixMax[i-1] <= nums[i] and nums[i] <= suffixMin[i+1]. This converts repeated scans into constant-time checks per index. The idea relies on tracking global constraints around each position rather than re-evaluating neighbors repeatedly. Time complexity becomes O(n) with O(n) extra memory.
This problem combines reasoning about order constraints in an array with the guarantees required for binary search. Binary search assumes the array is sorted. Here you identify elements that already satisfy that assumption locally relative to the rest of the array.
Recommended for interviews: The prefix-max and suffix-min technique is the expected solution. Interviewers want to see that you transform repeated range checks into precomputed state using linear scans. Showing the brute force first demonstrates you understand the condition that defines a valid element, while the optimized approach shows you can reduce the complexity from O(n²) to O(n).
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Validation | O(n²) | O(1) | Useful for understanding the binary-search condition or very small arrays |
| Prefix Max + Suffix Min | O(n) | O(n) | General case; optimal approach used in interviews and production |