If you are here, then you are facing problems in solving the Subarray with a given XOR problem.
Programmers are facing a lot of problems in solving the questions related to Subarray. It is because they are having issues with understanding the concept of Subarray and XOR.
If you are not having enough information about both the term “Subarray” and “XOR”, then you will be facing a lot of difficulty in solving the most important problems like Subarray with given XOR, sum tree, subarray with the given sum, etc.
Whether you are a newbie or a programmer who is not having the essential information related to the Subarray, this guide is for you. Here you will find all the related information through which you will be able to code the problem “Subarray with given XOR”.
What is XOR Subarray?
Before starting with the solution, you need to understand the concept of XOR. Without knowing about the term XOR, you will not be able to solve the problem of the Subarray with given XOR or other problems of DSA like subarray with a given sum, sum tree, etc.
Let’s start with the term “XOR”. You all have studied the binary representation number during your course of B.TECH, BCA, MCA, or other. The XOR is based on it. For finding the XOR of a given number, we write its binary number which is understood by the machine language.
After writing the binary number, we will compare it with another binary number. Once we have compared both of them, then we will check whether the binary number of both the given number is the same or not. If the number 0 and 1 or 1 and 0, then we will write their output 1 and 0 as both are different, and if the number is the same 0 and 0 or 1 and 1, then we will write its value 0 and 0.
By following the same concept we will be finding the XOR Subarray.
Why should I learn XOR Subarray?
A lot of reasons are available behind learning the concept of XOR and XOR Subarray. The XOR subarray problems are asked on various coding platforms so that they can get to know about the approach to how to solve it. Through this, you will be able to answer the recruiter if the question is asked in the interview.
Many companies are looking for those candidates who are having proper knowledge of Data Structure and algorithms and you need to have basic knowledge of the problems like Subarray with given XOR, Subarray with a given sum, sum tree, and much more.
So, it will be best for you, if you know all the data structures. After that, there will be a lot of opportunities for you to apply.
Now, you have got all the valid points so why should you learn the XOR subarray? Let’s check what approach we should follow to solve the problem of the XOR subarray.
How To Solve Problem of Subarray with given XOR
The problem statement of the question is that you will have to find all the subarrays whose XOR is equal to the given XOR which will be mentioned in the question.
Problem: int arr[]= {4,2,2,6,4}; k= 6;
Approach
- First of all, we will iterate over the array with two loops. One for loop will start from index 0 and the other will from 1.
- We will compare both the values with their binary values and will convert them into the XOR.
- After it, we will compare it with the given XOR. Now, if the XOR is equal to the given XOR, then we have got the subarray.
- Again we will do this for finding the other subarray if they are having the XOR or not.
- Once we are done, then we will end the for a loop.
Dry Run
Let’s do the dry run to check whether the approach that we will take is correct or not.
- First of all, we will start the iteration and will check the xor for the subarray. When we will be comparing the binary number of both iterations, then we will also check with the given XOR whether they are equal or not.
- If they are equal then we will stop the iteration, otherwise, we will keep it running.
- For the first two elements, we have 4 and 2. Let’s check the XOR:
8 4 2 1
4: 0 1 0 0
2: 0 0 1 0
XOR: 0 1 1 0
The XOR which we have got is of bit 4 and 2 which makes 6. So, when we compare it with the given XOR, then it is equivalent. So, we have found the first subarray with the given XOR.
- Now, we will again start the iteration and will check whether there is any more subarray or not.
- This time, we will check for the elements 4,2,2,6,4. We will convert the given elements of the subarray to the binary and will check whether it is an XOR subarray or not.
8 4 2 1
4: 0 1 0 0
2: 0 0 1 0
XOR: 0 1 1 0
2: 0 0 1 0
XOR: 0 1 0 0
6: 0 1 1 0
XOR: 0 0 1 0
4: 0 1 0 0
XOR: 0 1 1 0
We have got the XOR on converting it to the number we will get 6 which is equivalent to the given XOR. So, we have found the next subarray.
- Now, we will repeat the steps and will check for it till the end.
We hope that you have got the right approach and the concept behind solving the problem.
Conclusion
Solving problems related to DSA will require a good understanding of the basic concepts of the topic.
If you don’t have the basic knowledge of the problem, then you will be not able to solve it. Start practicing and learning the different data structures to get placed at a good MNC company.