Sansa and XOR

  • + 0 comments

    Rust best solution

    If you’re looking for solutions to the 3-month preparation kit in either Python or Rust, you can find them below: my solutions

    fn sansa_and_xor(arr: &[i32]) -> i32 {
        //Time complexity: O(n)
        //Space complexity (ignoring input): O(1)
        //A number will appear in {(index+1)*(n-index)} subsequences
        //In case arr.len() is par, the appearences of any number will be par as well
        if arr.len() % 2 == 0 {
            return 0;
        }
        let mut xor_value = 0;
        for index in 0..arr.len() {
            let appearences = (index + 1) * (arr.len() - index);
            if appearences % 2 != 0 {
                xor_value ^= arr[index]
            }
        }
        return xor_value;
    }