🔥Beginner Friendly || Brute Force || Optimization Approach Both (Time And Space)
Method : 1 BruteForce(Time Limit Exceed\n\n# Intuition\nThe code aims to generate a sequence of integers that, when XORed together, produces the given vector pr
#                                                 Method : 1 BruteForce(Time Limit Exceed\n\n# Intuition\nThe code aims to generate a sequence of integers that, when XORed together, produces the given vector pref. It does this by iteratively finding the next integer for the sequence, starting with the first element of pref. The code uses a nested loop to incrementally test and identify the missing numbers in the sequence, adding them to the ans vector. While the approach is functional, the nested loop\'s high time complexity may result in inefficiency for larger inputs.\n\n# Complexity\n- Time complexity: O(n * 1000000000), where n is the size of the pref vector\n- Space complexity: The space complexity is O(n) as it stores the ans vector of the same size as pref\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findArray(vector<int>& pref) \n    {\n        int n = pref.size() ;\n        vector<int> ans ;\n        \n        ans.push_back(pref[0]) ;\n        int temp = pref[0] ;\n\n        for(int i = 1 ; i < n ; i++)\n        {\n            for(int num = 0 ; num <= 1000000000 ; num++)\n            {\n                if( (temp^num) == pref[i])\n                {\n                    ans.push_back(num) ;\n                    temp = temp^num ;\n                    break ;\n                }  \n            }\n        }\n        return ans ;   \n    }\n};\n```\n---\n#                                                 Method : 2 Time Optimization(accepted) \n\n# Intuition\nThe intuition behind this code is to perform a sequence of XOR operations on the input vector pref to generate the output vector ans. Each element of the output vector is formed by XORing the corresponding element in the input vector with the previous element in the input vector. \n\nThe provided image illustrates a problem-solving approach that is based on visual cues\n\n![IMG_20231031_132856.jpg](https://assets.leetcode.com/users/images/a684e674-e69a-4b3b-8871-6eeb1ad73e84_1698739185.7952168.jpeg)\n\n\n# Approach\n1. The loop starts from i = 1 and continues until i is less than the size of pref.\n2. In each iteration, it calculates the XOR of the current element pref[i-1] and the previous element pref[i-1], and appends the result to ans.\n3. Finally, it returns the ans vector as the result.\n\n# Complexity\n- Time complexity: O(n), where n is the size of the pref vector,\n- Space complexity: The space complexity is O(n) as well because it creates a new vector ans of the same size as pref\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findArray(vector<int>& pref) \n    {\n        int n = pref.size() ;\n        vector<int> ans ;\n        \n        ans.push_back(pref[0]) ;\n\n        for(int i = 1 ; i < n ; i++)\n           ans.push_back(pref[i-1]^pref[i]) ;\n\n        return ans ;   \n    }\n};\n```\n---\n#                                                 Method : 3 Time and Space Optimization(accepted) \n\n# Intuition\n1. It initializes n as the size of the input vector pref.\n2. The code then iterates through the elements of pref in reverse order (from the last element to the second element).\n3. In each iteration, it calculates the XOR of the current element pref[i] with the previous element pref[i-1] and updates \n   the current element with the result.\n4. The code returns the modified pref vector after all iterations.\n\n# Complexity\n- Time complexity: O(n)\n- Space complexity: O(1)\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findArray(vector<int>& pref) \n    {\n        int n = pref.size() ;\n        \n        for(int i = n-1 ; i > 0 ; i--)\n           pref[i] = pref[i]^pref[i-1] ;\n\n        return pref ;   \n    }\n};\n```\n\n** If you find it helpful, your upvote would be greatly appreciated. It provides valuable support to me. Thank you**
['Bit Manipulation', 'C++']