-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRearrangeArray
More file actions
46 lines (38 loc) · 1.13 KB
/
RearrangeArray
File metadata and controls
46 lines (38 loc) · 1.13 KB
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
import java.util.*;
public class Arrays_Rearrange {
static void rearrangeArray(int arr[], int n)
{
Arrays.sort(arr);
int[] tempArr = new int[n]; // To store modified array
int ArrIndex = 0;
for (int i = 0, j = n-1; i <= n / 2 || j > n / 2;
i++, j--)
{
if(ArrIndex < n)
{
tempArr[ArrIndex] = arr[i];
ArrIndex++;
}
if(ArrIndex < n)
{
tempArr[ArrIndex] = arr[j];
ArrIndex++;
}
}
for (int i = 0; i < n; i++)
arr[i] = tempArr[i];
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int num=sc.nextInt()
int arr[] = new int[num];
int n = arr.length;
for(int i=0;i<num;i++){
arr[i]=sc.nextInt();
}
rearrangeArray(arr, n);
for (int i = 0; i < n; i++)
System.out.print(arr[i]+" ");
}
}