Explain me the given solution that what approach it is and how this approach came in his mind

/*
Time Complexity: O(Q)
Space Complexity: O(Q),

Where ‘Q’ is the number of queries
*/

vector xorQuery(vector<vector> &queries)
{
// Create an empty array ans
vectorans;

// Create an array xorArray of the size of 10^5+1 initialized with 0
vector<int> xorArray(100001, 0);

// Iterate over all the queries
for (int i = 0; i < queries.size(); i++)
{
	if (queries[i][0] == 1)
	{
		ans.push_back(queries[i][1]);
	}
	else
	{
		xorArray[0] ^= queries[i][1];
		xorArray[ans.size()] ^= queries[i][1];
	}

}

// Computing cumulative prefix XOR and evaluating the answer
for (int i = 0; i < ans.size(); i++)
{
	if (i == 0)
	{
		ans[i] = ans[i] ^ xorArray[i];
	}
	else
	{
		xorArray[i] ^= xorArray[i - 1];
		ans[i] ^= xorArray[i];
	}

}

return ans;

}

hi @rjhk6767_a9801e8bdca93439,
the overall idea is not to perform o(n) operation when a xor query comes (which you r doing here just once for all query in second for loop)
in first for loop you are maintaining what values you have to perform operation (type 1 query), in between any type 2 query comes you are just keeping that information in second vector initially all 0.
Now why we only xor first and element after last (i.e 0 and ans.size() )element in xorArray?
lets understand with simple eg {1,2},{1,3}, {1,4}, {2,2}
2,3,4 comes store in ans array as they are of type 1
now type 2 query with 2 comes we store it in xorArray first and after last index (to nullify effect) which becomes 2 0 0 2.
we have now type 1 info (which elements) + type 2 info (to xor with)
so use for loop to and take cumulative xor.
when it reaches after last index in general it will be for here 2^2 i.e 0 which nullifies the effect of this xor for further array if there as it says to xor elements till that time.

hope this clears the doubt

@rjhk6767_a9801e8bdca93439 regarding how it came in mind is range questions are mostly easily done by cumulative sum or cumulative xor here. Hope u have solved question asking range sum in array

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.