fork download
  1. import java.util.Arrays;
  2.  
  3. class GfG {
  4.  
  5. // function to reverse an array
  6. static void reverseArray(int[] arr) {
  7. int n = arr.length;
  8.  
  9. // Temporary array to store elements in reversed order
  10. int[] temp = new int[n];
  11.  
  12. // Copy elements from original array to temp in reverse order
  13. for (int i = 0; i < n; i++)
  14. temp[i] = arr[n - i - 1];
  15.  
  16. // Copy elements back to original array
  17. for (int i = 0; i < n; i++)
  18. arr[i] = temp[i];
  19. }
  20.  
  21. public static void main(String[] args) {
  22. int[] arr = { 1, 4, 3, 2, 6, 5 };
  23.  
  24. reverseArray(arr);
  25.  
  26. for (int i = 0; i < arr.length; i++)
  27. System.out.print(arr[i] + " ");
  28. }
  29. }
Success #stdin #stdout 0.12s 53628KB
stdin
8 
1 2 3 4 5 6 7 8
stdout
5 6 2 3 4 1