What it does
Finds a pair in a list that has the closest sum to a given number. The list can be an Array or Vector. The code is simple and free of recursion.
It expects a sorted list. Time complexity O(N). Space complexity O(1).
Features
-
Multiple types are supported, including floating points. But
usize,u64,u128,i64,i128andf64are not supported due to f64 cannot exactly capture these values, which is relevant in this context because various types are attemped to be converted intof64as a general type in this crate.All other types are supported, eg.
i8,u8,i16,u16,i32,u32andf32.So, if your variable type is
u64ori64, you should cast them tou32andi32respectively before passing it to the function. -
Expressive and clean code.
version note: remove unnecessary bloat and improved performance.
How to use
This crate exports a function called find_pair that expects a reference to a sorted list (can be vector or array),and your desired sum (The sum you are looking for).
If there is no exact match, it will return the closest sum possible.
Please make sure to sort your list before passing it as an argument to the function.
Quick Start
use find_pair;
Example with floating point:
use find_pair;