XOR of Array Elements Except Itself in Java26 Sept 2024 | 5 min read It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to discuss XOR of array elements except itself in Java with different approaches and logic. Also, we will create Java programs for the same. Problem StatementWe have given an array that contains only non-negative elements. The task is to create another array (say output[]) such that output[i] contains XOR of all the elements of the input array except input[i]. The following examples illustrates the same. Let's understand it through examples. Example 1: Input: int input[] = {4, 7, 9, 3, 2, 5, 6} Output: output[] = {12, 15, 1, 11, 10, 13, 14} Explanation: If we exclude the current element and apply the XOR operation on rest of the elements, we get the mentioned output. Observe the following. output[0] = 7 ^ 9 ^ 3 ^ 2 ^ 5 ^ 6 = 12 output[1] = 4 ^ 9 ^ 3 ^ 2 ^ 5 ^ 6 = 15 output[2] = 4 ^ 7 ^ 3 ^ 2 ^ 5 ^ 6 = 1 output[3] = 4 ^ 7 ^ 9 ^ 2 ^ 5 ^ 6 = 11 output[4] = 4 ^ 7 ^ 9 ^ 3 ^ 5 ^ 6 = 10 output[5] = 4 ^ 7 ^ 9 ^ 3 ^ 2 ^ 6 = 13 output[6] = 4 ^ 7 ^ 9 ^ 3 ^ 2 ^ 5 = 14 Example 2: Input: int input[] = {0, 6, 2, 9} Output: output[] = {13, 11, 15, 4} Explanation: output[0] = 6 ^ 2 ^ 9 = 13 output[1] = 0 ^ 2 ^ 9 = 11 output[2] = 0 ^ 6 ^ 9 = 15 output[3] = 0 ^ 6 ^ 2 = 4 Solution to the ProblemNaive ApproachIn this approach, we will use a nested for loop for computing the XOR of all the elements except itself. FileName: XorArrElements.java Output: For the input array: 4 7 9 3 2 5 6 The output array is: 12 15 1 11 10 13 14 For the input array: 0 6 2 9 The output array is: 13 11 15 4 Complexity Analysis: The program uses the nested for-loop. Hence, the time complexity of the above program is O(n2), where n is the total number of elements present in the input array. The program is not using any extra space. Thus, the space complexity of the program is constant, i.e., O(1). The time complexity of the program can be reduced further. The following approach shows the same. Computing XOR of All The ElementsWe know that the XOR of the two numbers is always zero if both numbers are equal. Thus, we can compute the XOR of all the elements, and to exclude any element, do the XOR of all the elements with that element. Let's understand it with the help of an example. int arr[] = {a1, a2, a3, a4, a5, a6}, size = 6 Thus, output[0] = (a1 ^ a2 ^ a3 ^ a4 ^ a5 ^ a6) ^ a1, We know that the XOR operation is commutative. Therefore, we can group a1 together output[0] = (a1 ^ a1 ^ a2 ^ a3 ^ a4 ^ a5 ^ a6) = 0 ^ a2 ^ a3 ^ a4 ^ a5 ^ a6 (as a1 ^ a1 = 0) which is nothing but the XOR of all the elements except the element itself. The following program used the same concept. FileName: XorArrElements1.java Output: For the input array: 4 7 9 3 2 5 6 The output array is: 12 15 1 11 10 13 14 For the input array: 0 6 2 9 The output array is: 13 11 15 4 Complexity Analysis: The time complexity of the program is O(n), where n is the total number of elements present in the input array. The space complexity of the program is the same as the above. |
In this program, we will create a circular linked list and delete a node from the end of the list. If the list is empty, it will display the message "List is empty". If the list is not empty, we will loop through the list...
6 min read
In this program, we need to create a doubly linked list and rotate it by n node. This can be achieved by maintaining a pointer that starts from the head node and traverses the list until current points to the nth node. Move the list...
7 min read
Java Convert String to char We can convert String to char in java using charAt() method of String class. The charAt() method returns a single character only. To get all characters, you can use loop. Signature The charAt() method returns a single character of specified index. The signature of charAt()...
2 min read
In this program, we need to check whether a given string is palindrome or not. A string is said to be palindrome if it is the same from both the ends. For e.g. above string is a palindrome because if we try to read it from...
2 min read
In this program, we will create a doubly linked list and remove the duplicate, if present, by traversing through the list. Original List: List after removing duplicates: In above list, node2 is repeated thrice, and node 3 is repeated twice. Current will point to head, and index will...
7 min read
Java Convert float to String We can convert float to String in java using String.valueOf() and Float.toString() methods. Scenario It is generally used if we have to display float value in textfield because everything is displayed as a string in form. 1) String.valueOf() The String.valueOf() is an overloaded method. It can...
1 min read
In this program, we create a circular linked list and search a node in the list. 9->5->2->7->3 Consider, above example. Suppose we need to search for node 5. To solve this problem, we will iterate through the list and compare each node with 5. If match is...
5 min read
In this program, we need to sort the given array in ascending order such that elements will be arranged from smallest to largest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected...
4 min read
Java Convert double to int We can convert double to int in java using typecasting. To convert double data type into int, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype). Here, we are going to learn how to convert double primitive type into...
1 min read
In this program, we need to find out the maximum width of the binary tree. The width of the binary tree is the number of nodes present in any level. So, the level which has the maximum number of nodes will be the maximum width...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India