- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Find array using different XORs of elements in groups of size 4 in Java

We are given with an integer array of size N(size of multiple 4) and we have to perform Xclusive OR operation on the array such that input[1- 4] resembles utility_arr[1- 4] and the conditions of computing is If arr[1 – 4] = {a1, a2, a3, a4} then q[1 – 4] = {a1 ⊕ a2 ⊕ a3, a1 ⊕ a2 ⊕ a4, a1 ⊕ a3 ⊕ a4, a2 ⊕ a3 ⊕ a4}

Let us see various input output scenarios for this -

**In **− int[] input = { 5, 2, 3, 4 };

**Out **− Result after XOR operation 4 3 2 5

**Explanation **−An Exclusive-OR gate's output only goes "HIGH" when both of its input terminals are at "DIFFERENT" logic levels from one another. The output is a "0" if these two inputs, A and B, are both at logic level "1" or "0," making the gate a "odd but not the even gate." In other words, when the inputs have an odd number of 1s, the output is "1."

a1 ⊕ a2 ⊕ a3 = 5 ⊕ 2 ⊕ 3 = 4

a1 ⊕ a2 ⊕ a4 = 5 ⊕ 2 ⊕ 4 = 3

a1 ⊕ a3 ⊕ a4 = 5⊕ 3 ⊕ 4 = 2

a2 ⊕ a3 ⊕ a4 = 2 ⊕ 3 ⊕ 4 = 5

**In **− int[] input = { 7, 6, 4, 4, 3, 8, 9, 5 };

**Out **− Result after XOR operations 5 5 7 6 2 14 15 4

**Explanation **− An Exclusive-OR gate's output only goes "HIGH" when both of its input terminals are at "DIFFERENT" logic levels from one another. The output is a "0" if these two inputs, A and B, are both at logic level "1" or "0," making the gate a "odd but not the even gate." In other words, when the inputs have an odd number of 1s, the output is "1." Will only work for input[] of size multiples of 4, other sized input arrays will show 0s in place of odd placed numbers.

## Result after XOR operations 5 5 7 6 2 14 15 4

## Approach used in the below program is as follows −

According to the properties of XOR a ⊕ a = 0 and a ⊕ 0 = a. (a ⊕ b ⊕ c) ⊕ (b ⊕ c ⊕ d) = a ⊕ d (As (b ⊕ c) ⊕ (b ⊕ c) = 0)

For computation the array is divided into groups of 4 and we will follow the properties of XOR to calculate the results of each group.

Taking reference from the above property using (a ⊕ d) we can calculate b and c (a ⊕ b ⊕ d) ⊕ (a ⊕ d) = b (a ⊕ c ⊕ d) ⊕ (a ⊕ d) = c

And by using b and c we can get a and d by using the following approach (a ⊕ b ⊕ c) ⊕ (b) ⊕ (c) = a (b ⊕ c ⊕ d) ⊕ (b) ⊕ (c) = d

The process is repeated for all four groups

A loop is iterated with 2 pointers i and j till length of the array divided by four and a temp value(ans) and an utility array(which stores answers) is introduced.

Inside the for loop following xor operations are implemented

ans= input array[i] ⊕ input array[i+3]

Utility array[i+1](calculating b)= input array[i+1] ⊕ ans

Utility array[i+2](calculating c)= input array[i+2] ⊕ ans

Utility array[i](calculating a)= input array[i] ⊕ ((Utility array[i + 1]) ^ (Utility array[i + 2]))

Utility array[i](calculating d)= input array[i+3] ⊕ ((Utility array[i + 1]) ^ (Utility array[i + 2]))

And the pointer is updated for next set of four characters

Finally, the array is printed and the result is returned to the user.

## Example

import java.util.Arrays; import java.util.List; public class Tutorials{ static int ans = 0; public static void main(String args[]){ int[] input = {7, 1, 2, 3}; int[] arr = new int[input.length]; for (int i = 0, j = 0; j < input.length / 4; j++){ ans = input[i] ^ input[i + 3]; arr[i + 1] = input[i + 1] ^ ans; arr[i + 2] = input[i + 2] ^ ans; arr[i] = input[i] ^ ((arr[i + 1]) ^ (arr[i + 2])); arr[i + 3] = input[i + 3] ^ (arr[i + 1] ^ arr[i + 2]); i += 4; } System.out.println("Different XORs of elements in groups of size 4 is: "); for (int i = 0; i < arr.length; i++){ System.out.println(arr[i]); } } }

## Output

If we run the above code it will generate the following Output

Different XORs of elements in groups of size 4 is : 4 5 6 0

- Related Questions & Answers
- Java program to reverse an array in groups of given size
- Find elements of array using XOR of consecutive elements in C++
- Python program to reverse an array in groups of given size?
- Find size of array in C/C++ without using sizeof
- Change the size of Bootstrap input groups
- Reverse a Linked List in groups of a Given Size using C++
- Maximum product quadruple (sub-sequence of size 4) in array in C++
- XOR of all subarray XORs in C++
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Parts of array with n different elements in JavaScript
- Replace a C# array with a new array of different size
- Find minimum possible size of array with given rules for removing elements in C++
- Find the size of a HashMap in Java
- Java program to find the sum of elements of an array
- Maximum number of groups of size 3 containing two type of items in C++