diff options
author | Jack Sangdahl <[email protected]> | 2025-01-16 21:43:38 -0700 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-01-16 21:43:38 -0700 |
commit | 88de92a17bb68635921e1625d5b3063e26ea9c03 (patch) | |
tree | 6553be7c116fdd6997932103e8b308b9132bd533 | |
parent | 14aac43ae43413bb8d6d04bee8c342df2e8b2669 (diff) | |
download | leetcode-88de92a17bb68635921e1625d5b3063e26ea9c03.tar.gz leetcode-88de92a17bb68635921e1625d5b3063e26ea9c03.zip |
2683. neighbouring bitwise xor
-rw-r--r-- | neighbouring_bitwise_xor.c | 27 | ||||
-rw-r--r-- | neighbouring_bitwise_xor.py | 30 |
2 files changed, 57 insertions, 0 deletions
diff --git a/neighbouring_bitwise_xor.c b/neighbouring_bitwise_xor.c new file mode 100644 index 0000000..95b7de4 --- /dev/null +++ b/neighbouring_bitwise_xor.c @@ -0,0 +1,27 @@ +// 2683. Neighboring Bitwise XOR +#include "leetcode.h" + +/* + * a 0-indexed array 'derived' with length 'n' is derived by computing the + * bitwise xor of adjacnet values in a binary array 'original' of length 'n'. + * specifically, for each index 'i' in the range '[0, n - 1]' if 'i = n - 1' + * then 'derived[i] = original[i] ^ original[0]', otherwise 'derived[i] = + * original[i] ^ original[i + 1]'. given an array 'derived', your task is to + * determine whether there exists a valid binary array 'original' that could + * have formed 'derived'. return true if such an array exists or false + * otherwise. + */ + +bool doesValidArrayExist(int *derived, int derivedSize) { + int ans = 0; + for (int i = 0; i < derivedSize; i++) + ans ^= derived[i]; + return !ans; +} + +int main() { + int d1[] = {1, 1, 0}, d2[] = {1, 1}, d3[] = {1, 0}; + printf("%d\n", doesValidArrayExist(d1, ARRAY_SIZE(d1))); // expect: 1 + printf("%d\n", doesValidArrayExist(d2, ARRAY_SIZE(d2))); // expect: 1 + printf("%d\n", doesValidArrayExist(d3, ARRAY_SIZE(d3))); // expect: 0 +} diff --git a/neighbouring_bitwise_xor.py b/neighbouring_bitwise_xor.py new file mode 100644 index 0000000..1e8412b --- /dev/null +++ b/neighbouring_bitwise_xor.py @@ -0,0 +1,30 @@ +# 2683. Neighboring Bitwise XOR +from functools import reduce +from operator import xor + +""" +a 0-indexed array 'derived' with length 'n' is derived by computing the +bitwise xor of adjacnet values in a binary array 'original' of length 'n'. +specifically, for each index 'i' in the range '[0, n - 1]' if 'i = n - 1' +then 'derived[i] = original[i] ^ original[0]', otherwise 'derived[i] = +original[i] ^ original[i + 1]'. given an array 'derived', your task is to +determine whether there exists a valid binary array 'original' that could +have formed 'derived'. return true if such an array exists or false +otherwise. +""" + + +class Solution(object): + def doesValidArrayExist(self, derived): + """ + :type derived: List[int] + :rtype: bool + """ + return not reduce(xor, derived) + + +if __name__ == "__main__": + obj = Solution() + print(obj.doesValidArrayExist(derived=[1, 1, 0])) + print(obj.doesValidArrayExist(derived=[1, 1])) + print(obj.doesValidArrayExist(derived=[1, 0])) |