**Swap Two Adjacent Elements in an Array:** This Java program takes an input array containing integers and swaps adjacent elements to create a modified array. The program achieves this by iterating through the array with a step size of 2 (i.e., taking every second element), and for each pair of adjacent elements, it swaps their positions.

### Swap Two Adjacent Elements in an Array

package com.softwaretestingo.interviewprograms; public class InterviewPrograms33 { /* * Input array- {1,2,3,4,5} * Output array- {2,1,4,3,5} */ public static void main(String[] args) { int[] arr = {1,2,3,4,5}; int tmp=0; for(int i =1;i<arr.length;i+=2) { tmp=arr[i]; arr[i]=arr[i-1]; arr[i-1]=tmp; } for(int i:arr) System.out.print(i); } }

Output

21435

Here’s a breakdown of the program:

- Define the main method:
- The entry point of the program is the main method.

- Initialize the input array:
- The input array arr is initialized with the values {1, 2, 3, 4, 5}.

- Swap adjacent elements:
- The program iterates through the array with a for loop, starting from index 1 (the second element) and incrementing by 2 at each step.
- For each iteration, it takes the current element arr[i] and swaps its position with the previous element arr[i-1] using a temporary variable tmp.

- Print the modified array:
- After processing all elements of the array and performing the swaps, the program prints the modified array elements.

Overall, this program swaps adjacent elements of the input array to create a modified array. For the given input array {1, 2, 3, 4, 5}, the output will be {2, 1, 4, 3, 5}, where adjacent elements have been swapped. The program demonstrates basic array manipulation and a simple for loop to achieve this task.

**Alternative Way 1:**

This Java program takes an input array containing integers and swaps adjacent elements to create a modified array. The program achieves this using a **for loop** and conditional statements to handle arrays of even and odd lengths.

package com.softwaretestingo.interviewprograms; import java.util.Arrays; public class InterviewPrograms33_1 { /* * Input array- {1,2,3,4,5} * Output array- {2,1,4,3,5} */ public static void main(String[] args) { int [] a = {1,2,3,4,5}; if ( a.length % 2 == 1 ) { for ( int i = 0 ; i<a.length-1; i=i+2) { int temp= a[i]; a[i]= a[i+1]; a[i+1]=temp; } } else if (a.length%2==0) { for (int i=0; i<a.length; i=i+2) { int temp= a[i]; a[i] = a[i+1]; a[i+1]= temp ; } } System.out.println(Arrays.toString(a)); } }

Output

[2, 1, 4, 3, 5]

Here’s a breakdown of the program:

- Define the main method:
- The entry point of the program is the main method.

- Initialize the input array:
- The input array a is initialized with the values {1, 2, 3, 4, 5}.

- Swap adjacent elements for arrays with odd length:
- The program first checks if the length of the array a is odd (i.e., a.length % 2 == 1).
- If the array length is odd, it means the last element is not paired with another element for swapping, so it iterates through the array from the beginning to the second-to-last element using a for loop with a step size of 2.
- For each iteration, it swaps the current element a[i] with the next element a[i+1] using a temporary variable temp.

- Swap adjacent elements for arrays with even length:
- If the array length is even (i.e., a.length % 2 == 0), it means all elements are paired for swapping, so it iterates through the array from the beginning to the last element using a for loop with a step size of 2.
- It swaps the current element a[i] with the next element a[i+1] using a temporary variable temp.

- Print the modified array:
- After processing all elements and performing the swaps, the program prints the modified array using Arrays.toString(a).

Overall, this program swaps adjacent elements of the input array to create a modified array. For the given input array {1, 2, 3, 4, 5}, the output will be {2, 1, 4, 3, 5}, where adjacent elements have been swapped. The program demonstrates basic array manipulation, conditional statements, and loops to achieve this task.