Introduction
Reversing an array in-place (i.e., without using an additional array) is a common problem in programming. This involves swapping elements from the start and end of the array until the middle of the array is reached. This guide will show you how to create a Java program that reverses an array in-place.
Problem Statement
Create a Java program that:
- Takes an array of integers as input.
- Reverses the array in-place without using another array.
- Returns and displays the reversed array.
Example 1:
- Input:
[1, 2, 3, 4, 5]
- Output:
[5, 4, 3, 2, 1]
Example 2:
- Input:
[10, 20, 30, 40, 50]
- Output:
[50, 40, 30, 20, 10]
Solution Steps
- Initialize the Array: Define an array with integer values.
- Reverse the Array In-Place:
- Use two pointers: one starting at the beginning and the other at the end of the array.
- Swap the elements at these two positions.
- Move the pointers towards the center of the array until they meet or cross each other.
- Display the Result: Print the reversed array.
Java Program
Java Program to Reverse an Array Without Using Another Array
import java.util.Arrays;
/**
* Java Program to Reverse an Array Without Using Another Array
* Author: https://www.javaguides.net/
*/
public class ReverseArrayInPlace {
public static void main(String[] args) {
// Step 1: Initialize the array
int[] array = {1, 2, 3, 4, 5};
// Step 2: Reverse the array in-place
reverseArray(array);
// Step 3: Display the reversed array
System.out.println("Reversed array: " + Arrays.toString(array));
}
// Method to reverse the array in-place
public static void reverseArray(int[] array) {
int left = 0;
int right = array.length - 1;
while (left < right) {
// Swap the elements at left and right pointers
int temp = array[left];
array[left] = array[right];
array[right] = temp;
// Move the pointers towards the center
left++;
right--;
}
}
}
Explanation
Input: The program uses a predefined array of integers.
Reversing the Array In-Place:
- The
reverseArray()
method takes an array as input. - Two pointers,
left
andright
, are initialized to point to the first and last elements of the array, respectively. - In a
while
loop, the elements at these pointers are swapped, and the pointers are moved towards the center of the array. - The loop continues until the
left
pointer is no longer less than theright
pointer, indicating that the array has been fully reversed.
- The
Output: The program prints the reversed array using
Arrays.toString()
for easy formatting.
Output Example
Example 1:
Reversed array: [5, 4, 3, 2, 1]
Example 2:
Reversed array: [50, 40, 30, 20, 10]
Explanation of Output:
- Example 1: The array
[1, 2, 3, 4, 5]
is reversed in-place to become[5, 4, 3, 2, 1]
. - Example 2: The array
[10, 20, 30, 40, 50]
is reversed in-place to become[50, 40, 30, 20, 10]
.
Conclusion
This Java program effectively reverses an array in-place without using another array. By using two pointers that start at opposite ends of the array and move towards the center, the program efficiently swaps elements to achieve the reversal. This approach is memory-efficient because it avoids the need for additional storage, making it suitable for scenarios where memory usage is a concern.
Comments
Post a Comment
Leave Comment