-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverserArrayInGroup.java
More file actions
34 lines (31 loc) · 901 Bytes
/
ReverserArrayInGroup.java
File metadata and controls
34 lines (31 loc) · 901 Bytes
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
package Arrays;
import java.util.ArrayList;
/**
* @author Vishal Singh */
public class ReverserArrayInGroup {
public static ArrayList<Integer> reverseInGroups(ArrayList<Integer> mv, int n, int k) {
for (int i = 0; i < n ; i=i+k) {
int l = i;
int r = Math.min(i + k - 1, n - 1);
int temp;
while (l<r){
temp = mv.get(l);
mv.set(l,mv.get(r));
mv.set(r,temp);
l++;
r--;
}
}
return mv;
}
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
System.out.println(reverseInGroups(arrayList,arrayList.size(),3));
}
}