Skip to main content

lower_bound

Function lower_bound 

Source
pub fn lower_bound<T: Ord>(list: &[T], target: &T) -> usize
Expand description

Birinchi >= target elementning indeksi (C++ dagi lower_bound).

Agar bunday element bo’lmasa, list.len() qaytadi — ya’ni “oxiriga qo’shish kerak”. Bu funksiya yordamida takrorlanuvchi qiymatlarning eng chap o’rnini topish mumkin.

  • Time: O(log n), Space: O(1).

§Misol

use rust_algorithms::searching::lower_bound;

let v = [1, 2, 2, 2, 5];
assert_eq!(lower_bound(&v, &2), 1); // birinchi 2
assert_eq!(lower_bound(&v, &3), 4); // 3 yo'q → 5 turgan joy
assert_eq!(lower_bound(&v, &9), 5); // hammasidan katta