Need Some Change - Java
Submissions: 4640   Accuracy:

25.89%

  Difficulty: Easy   Marks: 2
Associated Course(s):   Fork Java

Now, as you're familiar with operating on arrays, let's do an intricate task. Here, we'll do swapping of array elements.

Given an array A of N positive integers. The task is to swap every ith element of the array with (i+2)th element.

Input Format:
First line of input contains number of testcase T. For each testcase, there will be two lines, first of which contains size of array N and next line contains N space seperated positive integers.

Output Format:
For each testcase, print the modified array.

Constraints:
1 <= T <= 100
3 <= N <= 106
1 <= Ai <= 106

User Task:
Your task is to complete the function swapElements(), which should swap each ith element with (i+2)th element.

Example:
Input:

1
5
1 2 3 4 5

Output:
3 4 5 2 1

Explanation:
Testcase 1: Swapping 1 and 3, makes the array 3 2 1 4 5. Now, swapping 2 and 4, makes the array 3 4 1 2 5. Again, swapping 1 and 5, makes the array 3 4 5 2 1.

** For More Input/Output Examples Use 'Expected Output' option **

Author: Dhiman Mayank


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.