Binary Searchable Numbers in an Unsorted Array - Problem

Binary Searchable Numbers in an Unsorted Array

Imagine you have a modified binary search algorithm that works on unsorted arrays by randomly selecting pivots instead of always choosing the middle element. While this algorithm doesn't guarantee to find all values in an unsorted array, some values are guaranteed to be found regardless of which pivots are randomly selected.

Given an integer array nums with unique numbers (which may or may not be sorted), your task is to determine how many values are guaranteed to be found using this modified binary search, no matter which pivots are randomly chosen during the search process.

The Algorithm:

  • Start with the entire array
  • Randomly pick any element as pivot
  • If pivot equals target, return true
  • If pivot < target, remove pivot and all elements to its left
  • If pivot > target, remove pivot and all elements to its right
  • Repeat until array is empty or target is found

A number is binary searchable if it will always be found regardless of the random pivot selections made during the search.

Input & Output

example_1.py โ€” Basic Case
$ Input: [1, 3, 2, 4, 5]
โ€บ Output: 4
๐Ÿ’ก Note: Elements 1, 3, 4, and 5 are binary searchable. Element 2 is not because 3 > 2 but appears to the left of 2, violating the local sorted property.
example_2.py โ€” Fully Sorted Array
$ Input: [1, 2, 3, 4, 5]
โ€บ Output: 5
๐Ÿ’ก Note: All elements are binary searchable because the array is already sorted, so every element maintains the local sorted property.
example_3.py โ€” Reverse Sorted
$ Input: [5, 4, 3, 2, 1]
โ€บ Output: 1
๐Ÿ’ก Note: Only element 5 is binary searchable because it's the only element where all elements to its right are smaller (and there are no elements to its left).

Visualization

Tap to expand
Binary Searchable Elements AnalysisOriginal Array: [1, 3, 2, 4, 5]1โœ“ VALID3โœ“ VALID2โœ— INVALID4โœ“ VALID5โœ“ VALIDAnalysis for element 2 (Invalid):Left elements: [1, 3]Problem: 3 > 2, but 3 is to the left of 2This violates the local sorted property!Result: 4 binary searchable numbersElements 1, 3, 4, 5 maintain proper ordering
Understanding the Visualization
1
Identify Pattern
A binary searchable element must have all smaller elements to its left and all larger elements to its right
2
Check Each Element
For each position, verify the local sorted property holds
3
Count Valid Elements
Elements that satisfy the property are guaranteed to be found by the modified binary search
Key Takeaway
๐ŸŽฏ Key Insight: An element is binary searchable if it maintains the sorted property locally - all elements to its left must be smaller, and all elements to its right must be larger. This guarantees the modified binary search will always find it regardless of pivot selection.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nยฒ)

For each of n elements, we check all elements to its left and right in worst case

n
2n
โš  Quadratic Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 103
  • -104 โ‰ค nums[i] โ‰ค 104
  • All elements in nums are unique
Asked in
Google 28 Amazon 22 Meta 18 Microsoft 15
21.1K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen