-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSwapArray.java
46 lines (41 loc) · 1.14 KB
/
SwapArray.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
//{ Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-->0)
{
int n = Integer.parseInt(br.readLine().trim());
String[] S = br.readLine().trim().split(" ");
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(S[i]);
}
Solution obj = new Solution();
obj.swapElements(arr, n);
for(int i = 0; i < n; i++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
}
// } Driver Code Ends
//User function Template for Java
class Solution
{
public void swapElements(int[] arr, int n)
{
for(int i = 0; i < n - 2; i++){
arr[i] = arr[i] ^ arr[i + 2];
arr[i + 2] = arr[i] ^ arr[i + 2];
arr[i] = arr[i] ^ arr[i + 2];
}
}
}