Problem Description: Given an Array, reverse it
Solution
If we swap first element with last element, then 2nd element with 2nd last element, 3rd element and 3rd last element and so on, we will finally get a reversed array.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.arrays;
import java.util.Arrays;
public class ReverseArray {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10};
reverseArray(arr);
System.out.println(Arrays.toString(arr));
}
public static int[] reverseArray(int[] arr) {
int i=0,j=arr.length-1;
while(i<j) {
swap(arr, i, j);
i++;
j--;
}
return arr;
}
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}