Java program to reverse an array in groups of given size


An array can be reversed in groups of a given size by reversing the subarrays of the required size. In the given article, the program demonstrates a technique for reversing an array in groups of a specified size. This technique is useful in various computational tasks and algorithm design. The program takes an array and a group size as input and reverses the array in groups of that size. The original and modified arrays are then printed to the console.

Problem Statement

Write a Java program to reverse an array in groups of a given size. An example of this is given as follows:

Input

Original array = 1 2 3 4 5 6 7 8 9 10

Output

Modified array = 3 2 1 6 5 4 9 8 7 10

Steps to reverse an array in groups of given size

Below are the steps to reverse an array in groups of given size −

  • Initialize an array and a variable to store the group size.
  • Print the original array.
  • Use a for loop to iterate through the array in steps equal to the group size.
  • Within each iteration, use a while loop to reverse the subarray of the given size.
  • Print the modified array.

Java program to reverse an array in groups of given size

Here is a Java program that demonstrates how to reverse an array in groups of a specified size −

public class Example {
 public static void main(String[] args) {
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10};
int size = 4;
int n = arr.length;
System.out.print("Original array is: ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
for (int i = 0; i < n; i += size) {
 int left = i;
 int right = Math.min(i + size - 1, n - 1);
 int temp;
 while (left < right) {
temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
left += 1;
right -= 1;
 }
}
System.out.print("
Modified array is: "); for (int i = 0; i < n; i++) System.out.print(arr[i] + " ");  } }

Output

Original array is: 1 2 3 4 5 6 7 8 9 10
Modified array is: 4 3 2 1 8 7 6 5 10 9

Code Explanation

The program initializes an array arr with the values {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} and sets the group size to 4, indicating the size of the subarrays to be reversed. The length of the array is stored in the variable n. The original array is printed using a for loop that iterates through the array and prints each element. The program then reverses the array in groups of 4 elements using another for loop that steps through the array in increments of size. For each group, two pointers, left and right, are initialized at the start and end of the group, respectively. A while loop swaps the elements at these pointers, moving inward until left is no longer less than right.

After processing all groups, the modified array is printed using another for loop that iterates through the array and prints each element. The final output demonstrates the original and modified arrays, showing the subarrays reversed in groups of four elements each.

Conclusion

The program successfully reverses the array in groups of a given size. The output shows the original array and the modified array after reversing in groups of size 4. The program demonstrates an efficient way to reverse subarrays of a given size, which can be useful in various applications such as data processing and algorithm design.

Updated on: 24-Jul-2024

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements