-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinMaxRearrange.java
48 lines (40 loc) · 1.6 KB
/
MinMaxRearrange.java
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
38
39
40
41
42
43
44
45
46
47
//Given a sorted array
//of positive integers, rearrange the array alternately
// i.e first element should be a maximum value, at second position minimum value,
//at third position second max, at fourth position second min, and so on.
//Time and space complexity O(N)
public class MinMaxRearrange {
// Function to rearrange the array elements alternately
static void rearrange(long[] arr, int n) {
// Initialize indexes of the smallest and largest elements
int small = 0, large = n - 1;
// Maximum element value + 1 to ensure all elements in array are handled
long maxElem = arr[large] + 1;
// Traverse the array
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
// Even index: place the maximum element at the current position
arr[i] += (arr[large--] % maxElem) * maxElem;
} else {
// Odd index: place the minimum element at the current position
arr[i] += (arr[small++] % maxElem) * maxElem;
}
}
// Divide all elements by maxElem to get the rearranged array
for (int i = 0; i < n; i++) {
arr[i] /= maxElem;
}
}
// Driver code
public static void main(String[] args) {
long[] arr = { 1, 2, 3, 4, 5, 6 };
int n = arr.length;
System.out.println("Original Array");
for (long num : arr)
System.out.print(num + " ");
rearrange(arr, n);
System.out.println("\nModified Array");
for (long num : arr)
System.out.print(num + " ");
}
}