diff --git a/DIRECTORY.md b/DIRECTORY.md index 1c08021c544..eb6cdc1525e 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -21,13 +21,16 @@ * [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) + * [Rightmost Set Bit](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/rightmost_set_bit.rs) * [Sum of Two Integers](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/sum_of_two_integers.rs) * [Swap Odd and Even Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/swap_odd_even_bits.rs) * [Trailing Zeros](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/binary_count_trailing_zeros.rs) * [Two's Complement](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/twos_complement.rs) + * [Unique Number](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/find_unique_number.rs) * Ciphers * [AES](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs) * [Another ROT13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/another_rot13.rs) diff --git a/src/bit_manipulation/find_missing_number.rs b/src/bit_manipulation/find_missing_number.rs new file mode 100644 index 00000000000..1511ba63c2c --- /dev/null +++ b/src/bit_manipulation/find_missing_number.rs @@ -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 { + 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); + } +} diff --git a/src/bit_manipulation/find_unique_number.rs b/src/bit_manipulation/find_unique_number.rs new file mode 100644 index 00000000000..81c9ddfb32c --- /dev/null +++ b/src/bit_manipulation/find_unique_number.rs @@ -0,0 +1,85 @@ +/// Finds the unique number in a slice where every other element appears twice. +/// +/// This function uses the XOR bitwise operation. Since XOR has the property that +/// `a ^ a = 0` and `a ^ 0 = a`, all paired numbers cancel out, leaving only the +/// unique number. +/// +/// # Arguments +/// +/// * `arr` - A slice of integers where all elements except one appear exactly twice +/// +/// # Returns +/// +/// * `Ok(i32)` - The unique number that appears only once +/// * `Err(String)` - An error message if the input is empty +/// +/// # Examples +/// +/// ``` +/// # use the_algorithms_rust::bit_manipulation::find_unique_number; +/// assert_eq!(find_unique_number(&[1, 1, 2, 2, 3]).unwrap(), 3); +/// assert_eq!(find_unique_number(&[4, 5, 4, 6, 6]).unwrap(), 5); +/// assert_eq!(find_unique_number(&[7]).unwrap(), 7); +/// assert_eq!(find_unique_number(&[10, 20, 10]).unwrap(), 20); +/// assert!(find_unique_number(&[]).is_err()); +/// ``` +pub fn find_unique_number(arr: &[i32]) -> Result { + if arr.is_empty() { + return Err("input list must not be empty".to_string()); + } + + let result = arr.iter().fold(0, |acc, &num| acc ^ num); + Ok(result) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic_case() { + assert_eq!(find_unique_number(&[1, 1, 2, 2, 3]).unwrap(), 3); + } + + #[test] + fn test_different_order() { + assert_eq!(find_unique_number(&[4, 5, 4, 6, 6]).unwrap(), 5); + } + + #[test] + fn test_single_element() { + assert_eq!(find_unique_number(&[7]).unwrap(), 7); + } + + #[test] + fn test_three_elements() { + assert_eq!(find_unique_number(&[10, 20, 10]).unwrap(), 20); + } + + #[test] + fn test_empty_array() { + assert!(find_unique_number(&[]).is_err()); + assert_eq!( + find_unique_number(&[]).unwrap_err(), + "input list must not be empty" + ); + } + + #[test] + fn test_negative_numbers() { + assert_eq!(find_unique_number(&[-1, -1, -2, -2, -3]).unwrap(), -3); + } + + #[test] + fn test_large_numbers() { + assert_eq!( + find_unique_number(&[1000, 2000, 1000, 3000, 3000]).unwrap(), + 2000 + ); + } + + #[test] + fn test_zero() { + assert_eq!(find_unique_number(&[0, 1, 1]).unwrap(), 0); + } +} diff --git a/src/bit_manipulation/mod.rs b/src/bit_manipulation/mod.rs index 606dc81e89e..4c2347e538f 100644 --- a/src/bit_manipulation/mod.rs +++ b/src/bit_manipulation/mod.rs @@ -1,11 +1,14 @@ mod binary_coded_decimal; mod binary_count_trailing_zeros; mod counting_bits; +mod find_missing_number; mod find_previous_power_of_two; +mod find_unique_number; mod highest_set_bit; mod is_power_of_two; mod n_bits_gray_code; mod reverse_bits; +mod rightmost_set_bit; mod sum_of_two_integers; mod swap_odd_even_bits; mod twos_complement; @@ -13,11 +16,14 @@ 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::find_unique_number::find_unique_number; pub use self::highest_set_bit::find_highest_set_bit; pub use self::is_power_of_two::is_power_of_two; pub use self::n_bits_gray_code::generate_gray_code; pub use self::reverse_bits::reverse_bits; +pub use self::rightmost_set_bit::{index_of_rightmost_set_bit, index_of_rightmost_set_bit_log}; pub use self::sum_of_two_integers::add_two_integers; pub use self::swap_odd_even_bits::swap_odd_even_bits; pub use self::twos_complement::twos_complement; diff --git a/src/bit_manipulation/rightmost_set_bit.rs b/src/bit_manipulation/rightmost_set_bit.rs new file mode 100644 index 00000000000..f35bdd998b3 --- /dev/null +++ b/src/bit_manipulation/rightmost_set_bit.rs @@ -0,0 +1,201 @@ +/// Finds the index (position) of the rightmost set bit in a number. +/// +/// The index is 1-based, where position 1 is the least significant bit (rightmost). +/// This function uses the bitwise trick `n & -n` to isolate the rightmost set bit, +/// then calculates its position using logarithm base 2. +/// +/// # Algorithm +/// +/// 1. Use `n & -n` to isolate the rightmost set bit +/// 2. Calculate log2 of the result to get the 0-based position +/// 3. Add 1 to convert to 1-based indexing +/// +/// # Arguments +/// +/// * `num` - A positive integer +/// +/// # Returns +/// +/// * `Ok(u32)` - The 1-based position of the rightmost set bit +/// * `Err(String)` - An error message if the input is invalid +/// +/// # Examples +/// +/// ``` +/// # use the_algorithms_rust::bit_manipulation::index_of_rightmost_set_bit; +/// // 18 in binary: 10010, rightmost set bit is at position 2 +/// assert_eq!(index_of_rightmost_set_bit(18).unwrap(), 2); +/// +/// // 12 in binary: 1100, rightmost set bit is at position 3 +/// assert_eq!(index_of_rightmost_set_bit(12).unwrap(), 3); +/// +/// // 5 in binary: 101, rightmost set bit is at position 1 +/// assert_eq!(index_of_rightmost_set_bit(5).unwrap(), 1); +/// +/// // 16 in binary: 10000, rightmost set bit is at position 5 +/// assert_eq!(index_of_rightmost_set_bit(16).unwrap(), 5); +/// +/// // 0 has no set bits +/// assert!(index_of_rightmost_set_bit(0).is_err()); +/// ``` +pub fn index_of_rightmost_set_bit(num: i32) -> Result { + if num <= 0 { + return Err("input must be a positive integer".to_string()); + } + + // Isolate the rightmost set bit using n & -n + let rightmost_bit = num & -num; + + // Calculate position: log2(rightmost_bit) + 1 + // We use trailing_zeros which gives us the 0-based position + // and add 1 to make it 1-based + let position = rightmost_bit.trailing_zeros() + 1; + + Ok(position) +} + +/// Alternative implementation using a different algorithm approach. +/// +/// This version demonstrates the mathematical relationship between +/// the rightmost set bit position and log2. +/// +/// # Examples +/// +/// ``` +/// # use the_algorithms_rust::bit_manipulation::index_of_rightmost_set_bit_log; +/// assert_eq!(index_of_rightmost_set_bit_log(18).unwrap(), 2); +/// assert_eq!(index_of_rightmost_set_bit_log(12).unwrap(), 3); +/// ``` +pub fn index_of_rightmost_set_bit_log(num: i32) -> Result { + if num <= 0 { + return Err("input must be a positive integer".to_string()); + } + + // Isolate the rightmost set bit + let rightmost_bit = num & -num; + + // Use f64 log2 and convert to position + let position = (rightmost_bit as f64).log2() as u32 + 1; + + Ok(position) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_basic_cases() { + // 18 = 10010 in binary, rightmost set bit at position 2 + assert_eq!(index_of_rightmost_set_bit(18).unwrap(), 2); + + // 12 = 1100 in binary, rightmost set bit at position 3 + assert_eq!(index_of_rightmost_set_bit(12).unwrap(), 3); + + // 5 = 101 in binary, rightmost set bit at position 1 + assert_eq!(index_of_rightmost_set_bit(5).unwrap(), 1); + } + + #[test] + fn test_powers_of_two() { + // 1 = 1 in binary, position 1 + assert_eq!(index_of_rightmost_set_bit(1).unwrap(), 1); + + // 2 = 10 in binary, position 2 + assert_eq!(index_of_rightmost_set_bit(2).unwrap(), 2); + + // 4 = 100 in binary, position 3 + assert_eq!(index_of_rightmost_set_bit(4).unwrap(), 3); + + // 8 = 1000 in binary, position 4 + assert_eq!(index_of_rightmost_set_bit(8).unwrap(), 4); + + // 16 = 10000 in binary, position 5 + assert_eq!(index_of_rightmost_set_bit(16).unwrap(), 5); + + // 32 = 100000 in binary, position 6 + assert_eq!(index_of_rightmost_set_bit(32).unwrap(), 6); + } + + #[test] + fn test_odd_numbers() { + // All odd numbers have rightmost set bit at position 1 + assert_eq!(index_of_rightmost_set_bit(1).unwrap(), 1); + assert_eq!(index_of_rightmost_set_bit(3).unwrap(), 1); + assert_eq!(index_of_rightmost_set_bit(7).unwrap(), 1); + assert_eq!(index_of_rightmost_set_bit(15).unwrap(), 1); + assert_eq!(index_of_rightmost_set_bit(31).unwrap(), 1); + } + + #[test] + fn test_even_numbers() { + // 6 = 110 in binary, rightmost set bit at position 2 + assert_eq!(index_of_rightmost_set_bit(6).unwrap(), 2); + + // 10 = 1010 in binary, rightmost set bit at position 2 + assert_eq!(index_of_rightmost_set_bit(10).unwrap(), 2); + + // 20 = 10100 in binary, rightmost set bit at position 3 + assert_eq!(index_of_rightmost_set_bit(20).unwrap(), 3); + } + + #[test] + fn test_zero() { + assert!(index_of_rightmost_set_bit(0).is_err()); + assert_eq!( + index_of_rightmost_set_bit(0).unwrap_err(), + "input must be a positive integer" + ); + } + + #[test] + fn test_negative_numbers() { + assert!(index_of_rightmost_set_bit(-1).is_err()); + assert!(index_of_rightmost_set_bit(-10).is_err()); + assert_eq!( + index_of_rightmost_set_bit(-5).unwrap_err(), + "input must be a positive integer" + ); + } + + #[test] + fn test_large_numbers() { + // 1024 = 10000000000 in binary, position 11 + assert_eq!(index_of_rightmost_set_bit(1024).unwrap(), 11); + + // 1023 = 1111111111 in binary, position 1 + assert_eq!(index_of_rightmost_set_bit(1023).unwrap(), 1); + + // 2048 = 100000000000 in binary, position 12 + assert_eq!(index_of_rightmost_set_bit(2048).unwrap(), 12); + } + + #[test] + fn test_consecutive_numbers() { + // Testing a range to ensure correctness + assert_eq!(index_of_rightmost_set_bit(14).unwrap(), 2); // 1110 + assert_eq!(index_of_rightmost_set_bit(15).unwrap(), 1); // 1111 + assert_eq!(index_of_rightmost_set_bit(16).unwrap(), 5); // 10000 + assert_eq!(index_of_rightmost_set_bit(17).unwrap(), 1); // 10001 + } + + #[test] + fn test_log_version() { + // Test the alternative log-based implementation + assert_eq!(index_of_rightmost_set_bit_log(18).unwrap(), 2); + assert_eq!(index_of_rightmost_set_bit_log(12).unwrap(), 3); + assert_eq!(index_of_rightmost_set_bit_log(5).unwrap(), 1); + assert_eq!(index_of_rightmost_set_bit_log(16).unwrap(), 5); + } + + #[test] + fn test_both_implementations_match() { + // Verify both implementations give the same results + for i in 1..=100 { + assert_eq!( + index_of_rightmost_set_bit(i).unwrap(), + index_of_rightmost_set_bit_log(i).unwrap() + ); + } + } +}

AltStyle によって変換されたページ (->オリジナル) /