LeetCode Entry

3702. Longest Subsequence With Non-Zero Bitwise XOR

15.08.2026 medium 2026 kotlin rust

Max subsequence to max non-zero xor

3702. Longest Subsequence With Non-Zero Bitwise XOR medium substack youtube

https://dmitrysamoylenko.com/leetcode/

15.08.2026.webp

Join me on Telegram

https://t.me/leetcode_daily_unstoppable/1452

Problem TLDR

Max subsequence to max non-zero xor

Intuition

XOR is zero if all values are zero or if they are two equal aubsequencies. To make them non-equal just remove one number.

Approach

  • or simulate with brute force and look at the results

Complexity

  • Time complexity: \(O(n)\)

  • Space complexity: \(O(1)\)

Code

    fun longestSubsequence(n: IntArray) =
        if (n.all{it<1}) 0 else n.size-if (n.fold(0,Int::xor)>0)0 else 1
    pub fn longest_subsequence(n: Vec<i32>) -> i32 {
        (n.iter().any(|&x|x>0)as i32)*(n.len()as i32-(n.iter().fold(0,|a,b|a^b)<1)as i32)
    }

Comments