✅☑[C++/Java/Python/JavaScript] || 1 line Code || EXPLAINED🔥
PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n(Also explained in the code)\n\n\n1. It checks if the size of the pref vector is less than or equal to 1. I
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n**(Also explained in the code)**\n\n\n1. It checks if the size of the `pref` vector is less than or equal to 1. If so, it returns the pref vector as it is, as there\'s not enough information to reconstruct an original array in such cases.\n\n1. If the `pref` vector has more than one element, it initializes an empty vector called `ans` to store the reconstructed original array.\n\n1. It then pushes the first element of the `pref` vector onto the `ans` vector. This is done because the first element of the original array is the same as the first element of the prefix array.\n\n1. The code enters a loop that iterates through the elements of the `pref` vector starting from the second element (at index 1).\n\n1. Inside the loop, it reconstructs the original array element by element. To find the next element in the original array, it takes the `XOR` (bitwise exclusive OR) of the current prefix element (**at index i**) and the previous one (**at index i - 1**).\n\n1. The result of the `XOR` operation is then pushed onto the `ans` vector, which is essentially building the original array element by element.\n\n1. After the loop has processed all elements in the `pref` vector, the reconstructed original array is stored in the `ans` vector.\n\n1. The function returns the `ans` vector, which now contains the reconstructed original array based on the given prefix array.\n\n\n\n# Complexity\n- *Time complexity:*\n   $$O(n)$$\n    \n\n- *Space complexity:*\n   $$O(n)$$\n    \n\n\n# Code\n```C++ []\n\nclass Solution {\npublic:\n    // Function to find the original array from the prefix array.\n    vector<int> findArray(vector<int>& pref) {\n        // If the prefix array has one or zero elements, return it as is.\n        if (pref.size() <= 1) {\n            return pref;\n        }\n\n        vector<int> ans; // Initialize a vector to store the original array.\n        ans.push_back(pref[0]); // The first element in the original array is the same as the first element in the prefix array.\n\n        // Iterate through the prefix array to reconstruct the original array.\n        for (int i = 1; i < pref.size(); i++) {\n            // To find the next element in the original array, take the XOR (bitwise exclusive OR) of the current prefix element and the previous one.\n            ans.push_back(pref[i - 1] ^ pref[i]);\n        }\n\n        return ans; // Return the reconstructed original array.\n    }\n};\n\n\n```\n```C []\n\n\nint* findArray(int* pref, int prefSize, int* returnSize) {\n    if (prefSize <= 1) {\n        *returnSize = prefSize;\n        return pref;\n    }\n\n    int* ans = (int*)malloc(prefSize * sizeof(int));\n    ans[0] = pref[0];\n\n    for (int i = 1; i < prefSize; i++) {\n        ans[i] = pref[i - 1] ^ pref[i];\n    }\n\n    *returnSize = prefSize;\n    return ans;\n}\n\n\n\n```\n\n```Java []\n\n\npublic class Solution {\n    public int[] findArray(int[] pref) {\n        if (pref.length <= 1) {\n            return pref;\n        }\n\n        int[] ans = new int[pref.length];\n        ans[0] = pref[0];\n\n        for (int i = 1; i < pref.length; i++) {\n            ans[i] = pref[i - 1] ^ pref[i];\n        }\n\n        return ans;\n    }\n}\n\n\n```\n```python3 []\n\nclass Solution:\n    def findArray(self, pref):\n        if len(pref) <= 1:\n            return pref\n\n        ans = [pref[0]]\n        for i in range(1, len(pref)):\n            ans.append(pref[i - 1] ^ pref[i])\n\n        return ans\n\n```\n\n\n```javascript []\nclass Solution {\n    findArray(pref) {\n        if (pref.length <= 1) {\n            return pref;\n        }\n\n        const ans = [pref[0]];\n        for (let i = 1; i < pref.length; i++) {\n            ans.push(pref[i - 1] ^ pref[i]);\n        }\n\n        return ans;\n    }\n}\n\n\n```\n\n\n---\n\n\n\n# PLEASE UPVOTE IF IT HELPED\n\n---\n---\n\n\n---
['Array', 'Bit Manipulation', 'C', 'C++', 'Java', 'Python3', 'JavaScript']