aboutsummaryrefslogtreecommitdiff
path: root/2022/06/common.rs
blob: ed5360cfa93a9e04790bcb537895c6837d41e3de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn all_distinct<T: PartialEq>(a: &[T]) -> bool {
    for i in 0..a.len() {
        for j in i+1..a.len() {
            if a[i] == a[j] {
                return false;
            }
        }
    }
    return true;
}

pub fn first_index_n_distinct(a: &[u8], n: usize) -> usize {
    assert!(n > 0, "{} must be greater than 0", n);
    for i in 0..a.len()-n+1 {
        if all_distinct(&a[i..i+n]) {
            return i+n-1;
        }
    }
    panic!("Cannot find {} distinct in a row", n);
}

Generated with cgit - Back to sebastiano.tronto.net