Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/1734.html

1734. Decode XORed Permutation

Level

Medium

Description

There is an integer array perm that is a permutation of the first n positive integers, where n is always odd.

It was encoded into another integer array encoded of length n - 1, such that encoded[i] = perm[i] XOR perm[i + 1]. For example, if perm = [1,3,2], then encoded = [2,1].

Given the encoded array, return the original array perm. It is guaranteed that the answer exists and is unique.

Example 1:

Input: encoded = [3,1]

Output: [1,2,3]

Explanation: If perm = [1,2,3], then encoded = [1 XOR 2,2 XOR 3] = [3,1]

Example 2:

Input: encoded = [6,5,4,6]

Output: [2,4,1,5,3]

Constraints:

  • 3 <= n < 10^5
  • n is odd.
  • encoded.length == n - 1

Solution

Use the condition that n is odd to solve this problem. First calculate totalXOR as the XOR from 1 to n. Next, calculate the XOR result of even indices of encoded as evenXOR, and the XOR result of odd indices of encoded as oddXOR. Then there is decoded[0] = totalXOR ^ oddXOR and decoded[n - 1] = totalXOR ^ evenXOR. Finally, for i from 0 to n - 2, there is decoded[i + 1] = decoded[i] ^ encoded[i].

  • class Solution {
        public int[] decode(int[] encoded) {
            int length = encoded.length;
            int n = length + 1;
            int totalXOR = 0;
            for (int i = 1; i <= n; i++)
                totalXOR ^= i;
            int[] decoded = new int[n];
            int evenXOR = 0, oddXOR = 0;
            for (int i = 0; i < length; i++) {
                if (i % 2 == 0)
                    evenXOR ^= encoded[i];
                else
                    oddXOR ^= encoded[i];
            }
            decoded[0] = totalXOR ^ oddXOR;
            decoded[n - 1] = totalXOR ^ evenXOR;
            for (int i = 0; i < length; i++)
                decoded[i + 1] = decoded[i] ^ encoded[i];
            return decoded;
        }
    }
    
    ############
    
    class Solution {
        public int[] decode(int[] encoded) {
            int n = encoded.length + 1;
            int[] ans = new int[n];
            int a = 0;
            int b = 0;
            for (int i = 0; i < n - 1; i += 2) {
                a ^= encoded[i];
            }
            for (int i = 0; i < n + 1; ++i) {
                b ^= i;
            }
            ans[n - 1] = a ^ b;
            for (int i = n - 2; i >= 0; --i) {
                ans[i] = ans[i + 1] ^ encoded[i];
            }
            return ans;
        }
    }
    
  • // OJ: https://leetcode.com/problems/decode-xored-permutation/
    // Time: O(N)
    // Space: O(1)
    class Solution {
    public:
        vector<int> decode(vector<int>& A) {
            int N = A.size() + 1;
            vector<int> ans(N);
            for (int i = 0; i <= 16; ++i) {
                int target = 0, bit = 0, cnt = 0;
                for (int j = 1; j <= N; ++j) target += j >> i & 1;
                for (int n : A) {
                    bit ^= n >> i & 1;
                    cnt += bit;
                }
                ans[0] |= (cnt != target) << i;
                for (int j = 1; j < N; ++j) {
                    int x = A[j - 1] >> i & 1, prev = ans[j - 1] >> i & 1, b = x ^ prev;
                    ans[j] |= b << i;
                }
            }
            return ans;
        }
    };
    
  • class Solution:
        def decode(self, encoded: List[int]) -> List[int]:
            n = len(encoded) + 1
            a = b = 0
            for i in range(0, n - 1, 2):
                a ^= encoded[i]
            for i in range(n + 1):
                b ^= i
            ans = [a ^ b]
            for e in encoded[::-1]:
                ans.append(ans[-1] ^ e)
            return ans[::-1]
    
    ############
    
    # 1734. Decode XORed Permutation
    # https://leetcode.com/problems/decode-xored-permutation/
    
    class Solution:
        def decode(self, encoded: List[int]):
            curr = start = 0
            
            for i in range(1, len(encoded)+2):
                start ^= i
            
            for x in encoded:
                curr ^= x
                start ^= curr
            
            res = [start]
            for x in encoded:
                res.append(res[-1] ^ x)
            
            return res
    
  • func decode(encoded []int) []int {
    	n := len(encoded) + 1
    	ans := make([]int, n)
    	a, b := 0, 0
    	for i := 0; i < n-1; i += 2 {
    		a ^= encoded[i]
    	}
    	for i := 0; i < n+1; i++ {
    		b ^= i
    	}
    	ans[n-1] = a ^ b
    	for i := n - 2; i >= 0; i-- {
    		ans[i] = ans[i+1] ^ encoded[i]
    	}
    	return ans
    }
    

All Problems

All Solutions