- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java program to reverse an array in groups of given size
An array can be reversed in groups of given size by reversing the subarrays of the required size. An example of this is given as follows.
Array = 1 2 3 4 5 6 7 8 9 10 Group size = 3 Modified array = 3 2 1 6 5 4 9 8 7 10
A program that demonstrates this is given as follows.
Example
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
Now let us understand the above program.
First the original array is printed. Then a for loop is used to reverse the array in groups of given size i.e 4. The code snippet that demonstrates this is given as follows.
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; } }
Then the modified array is displayed. The code snippet that demonstrates this is given as follows.
System.out.print("
Modified array is: "); for (int i = 0; i < n; i++) System.out.print(arr[i] + " ");
- Related Articles
- Python program to reverse an array in groups of given size?
- Java program to reverse an array upto a given position
- Program to reverse linked list by groups of size k in Python
- Java program to reverse an array
- Reverse a Linked List in groups of a Given Size using C++
- Reverse a Doubly-Linked List in Groups of a Given Size using C++
- Java Program to double the size of an array
- Java Program to count inversions of size three in a given array
- Program to reverse an array up to a given position in Python
- Java Program to extend the size of an Integer array
- JavaScript Program For Reversing A Linked List In Groups Of Given Size
- C# program to reverse an array
- Find array using different XORs of elements in groups of size 4 in Java
- Java Program to get the reverse of an Integer array with Lambda Expressions
- C program to reverse an array elements

Advertisements