Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DIRECTORY.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# List of all files

## Src
## src
* Backtracking
* [All Combinations of Size K](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/all_combination_of_size_k.rs)
* [Graph Coloring](https://github.com/TheAlgorithms/Rust/blob/master/src/backtracking/graph_coloring.rs)
Expand All @@ -21,6 +21,7 @@
* [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs)
* [Highest Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/highest_set_bit.rs)
* [Is Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/is_power_of_two.rs)
* [Missing Number](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_missing_number.rs)
* [N Bits Gray Code](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/n_bits_gray_code.rs)
* [Previous Power of Two](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_previous_power_of_two.rs)
* [Reverse Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/reverse_bits.rs)
Expand Down
122 changes: 122 additions & 0 deletions src/bit_manipulation/find_missing_number.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/// Finds the missing number in a slice of consecutive integers.
///
/// This function uses XOR bitwise operation to find the missing number.
/// It XORs all expected numbers in the range [min, max] with the actual
/// numbers present in the array. Since XOR has the property that `a ^ a = 0`,
/// all present numbers cancel out, leaving only the missing number.
///
/// # Arguments
///
/// * `nums` - A slice of integers forming a sequence with one missing number
///
/// # Returns
///
/// * `Ok(i32)` - The missing number in the sequence
/// * `Err(String)` - An error message if the input is invalid
///
/// # Examples
///
/// ```
/// # use the_algorithms_rust::bit_manipulation::find_missing_number;
/// assert_eq!(find_missing_number(&[0, 1, 3, 4]).unwrap(), 2);
/// assert_eq!(find_missing_number(&[4, 3, 1, 0]).unwrap(), 2);
/// assert_eq!(find_missing_number(&[-4, -3, -1, 0]).unwrap(), -2);
/// assert_eq!(find_missing_number(&[-2, 2, 1, 3, 0]).unwrap(), -1);
/// assert_eq!(find_missing_number(&[1, 3, 4, 5, 6]).unwrap(), 2);
/// ```
pub fn find_missing_number(nums: &[i32]) -> Result<i32, String> {
if nums.is_empty() {
return Err("input array must not be empty".to_string());
}

if nums.len() == 1 {
return Err("array must have at least 2 elements to find a missing number".to_string());
}

let low = *nums.iter().min().unwrap();
let high = *nums.iter().max().unwrap();

let mut missing_number = high;

for i in low..high {
let index = (i - low) as usize;
missing_number ^= i ^ nums[index];
}

Ok(missing_number)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_missing_in_middle() {
assert_eq!(find_missing_number(&[0, 1, 3, 4]).unwrap(), 2);
}

#[test]
fn test_unordered_array() {
assert_eq!(find_missing_number(&[4, 3, 1, 0]).unwrap(), 2);
}

#[test]
fn test_negative_numbers() {
assert_eq!(find_missing_number(&[-4, -3, -1, 0]).unwrap(), -2);
}

#[test]
fn test_negative_and_positive() {
assert_eq!(find_missing_number(&[-2, 2, 1, 3, 0]).unwrap(), -1);
}

#[test]
fn test_missing_at_start() {
assert_eq!(find_missing_number(&[1, 3, 4, 5, 6]).unwrap(), 2);
}

#[test]
fn test_unordered_missing_middle() {
assert_eq!(find_missing_number(&[6, 5, 4, 2, 1]).unwrap(), 3);
}

#[test]
fn test_another_unordered() {
assert_eq!(find_missing_number(&[6, 1, 5, 3, 4]).unwrap(), 2);
}

#[test]
fn test_empty_array() {
assert!(find_missing_number(&[]).is_err());
assert_eq!(
find_missing_number(&[]).unwrap_err(),
"input array must not be empty"
);
}

#[test]
fn test_single_element() {
assert!(find_missing_number(&[5]).is_err());
assert_eq!(
find_missing_number(&[5]).unwrap_err(),
"array must have at least 2 elements to find a missing number"
);
}

#[test]
fn test_two_elements() {
assert_eq!(find_missing_number(&[0, 2]).unwrap(), 1);
assert_eq!(find_missing_number(&[2, 0]).unwrap(), 1);
}

#[test]
fn test_large_range() {
assert_eq!(find_missing_number(&[100, 101, 103, 104]).unwrap(), 102);
}

#[test]
fn test_missing_at_boundaries() {
// Missing is the second to last element
assert_eq!(find_missing_number(&[1, 2, 3, 5]).unwrap(), 4);
}
}
2 changes: 2 additions & 0 deletions src/bit_manipulation/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod binary_coded_decimal;
mod binary_count_trailing_zeros;
mod counting_bits;
mod find_missing_number;
mod find_previous_power_of_two;
mod highest_set_bit;
mod is_power_of_two;
Expand All @@ -13,6 +14,7 @@ mod twos_complement;
pub use self::binary_coded_decimal::binary_coded_decimal;
pub use self::binary_count_trailing_zeros::binary_count_trailing_zeros;
pub use self::counting_bits::count_set_bits;
pub use self::find_missing_number::find_missing_number;
pub use self::find_previous_power_of_two::find_previous_power_of_two;
pub use self::highest_set_bit::find_highest_set_bit;
pub use self::is_power_of_two::is_power_of_two;
Expand Down