Skip to content

Conversation

@AkarshanGupta
Copy link
Contributor

Description

This PR adds a generalized binary search implementation that operates over a numeric range using a monotonic boolean predicate, instead of searching for a concrete value in an array.

The existing binary search implementation was limited to exact value lookup. This enhancement abstracts the core logic, allowing the same binary search mechanism to be reused across multiple problem domains.

The original implementations remain unchanged to preserve backward compatibility.


Key Features

  • Algorithm: Generalized Binary Search (predicate-based)
  • Input: Search range [low, high] and a boolean predicate function f(x)
  • Output: Smallest value x such that f(x) returns true
  • Complexity:
    • Time: O(log N)
    • Space: O(1)
  • Reusability: Enables binary search for optimization problems, boundary finding, and non-array-based use cases
  • Documentation: README updated to describe the new utility

Contribution Type

  • New Algorithm
  • Bug Fix
  • Documentation Update

Additional Notes

This abstraction directly addresses the discussion in the linked issue and lays the groundwork for applying similar generalizations to other algorithms in the repository.

Closes #2703

@AkarshanGupta AkarshanGupta force-pushed the feat/generalized-binary-search branch from c26fa7a to 71668b2 Compare December 25, 2025 16:14
@keon keon merged commit f2a261a into keon:main Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generalizing binary search algorithm

2 participants