-
Notifications
You must be signed in to change notification settings - Fork 0
/
14_xoring-and-clearing.java
50 lines (44 loc) · 1.03 KB
/
14_xoring-and-clearing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// User function Template for Java
class Solution {
// Print the array
public void printArr(int n, int arr[]) {
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Set element to zero
public void setToZero(int n, int arr[]) {
for (int i = 0; i < n; i++) {
arr[i] = 0;
}
}
// XOR elements with its i
public void xor1ToN(int n, int arr[]) {
for (int i = 0; i < n; i++) {
arr[i] = arr[i] ^ i; // XOR Operation
}
}
}
//{ Driver Code Starts
import java.util.*;
public class GFG {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
Solution obj = new Solution();
obj.xor1ToN(n, arr);
obj.printArr(n, arr);
obj.setToZero(n, arr);
obj.printArr(n, arr);
}
sc.close();
}
}
// } Driver Code Ends