Skip to content

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

Adds a new function find_missing_number to the bit_manipulation module that finds the missing number in an array of consecutive integers.

Algorithm

Uses XOR bitwise operation to efficiently find the missing number:

  1. Finds the minimum and maximum values in the array
  2. XORs all expected numbers in the range [min, max] with actual numbers
  3. Since a ^ a = 0, all present numbers cancel out, leaving only the missing number

Implementation Details

  • Time Complexity: $O(n)$ - single pass to find min/max, then $O(n)$ for XOR operations
  • Space Complexity: $O(1)$ - constant extra space
  • Works with:
    • Unordered arrays
    • Negative numbers
    • Mixed positive and negative ranges
  • Proper error handling for edge cases (empty array, single element)

Examples

find_missing_number(&[0, 1, 3, 4]) // Returns 2
find_missing_number(&[-4, -3, -1, 0]) // Returns -2
find_missing_number(&[4, 3, 1, 0]) // Returns 2 (works with unordered)

Testing

  • ✅ All existing tests pass
  • ✅ New tests added for:
    • Missing number in middle of sequence
    • Unordered arrays
    • Negative numbers
    • Mixed negative and positive numbers
    • Edge cases (empty array, single element, two elements)
    • Large ranges
    • Missing at boundaries

Checklist

  • Code follows Rust style guidelines (cargo fmt)
  • Passes clippy lints (cargo clippy)
  • All tests pass (cargo test)
  • Documentation includes examples
  • Function is properly exported in module
  • Error handling implemented for invalid inputs
  • Works with negative numbers and unordered arrays

@codecov-commenter
Copy link

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.67%. Comparing base (7a261d7) to head (26c690f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #974      +/-   ##
==========================================
- Coverage   95.71%   95.67%   -0.04%     
==========================================
  Files         346      347       +1     
  Lines       22608    22665      +57     
==========================================
+ Hits        21639    21685      +46     
- Misses        969      980      +11     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

@siriak siriak merged commit a486aea into TheAlgorithms:master Dec 18, 2025
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-find-missing-number branch December 18, 2025 21:17
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.

3 participants