-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlternate Sorting.java
More file actions
74 lines (69 loc) · 1.6 KB
/
Alternate Sorting.java
File metadata and controls
74 lines (69 loc) · 1.6 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Ramesh loves mathematics a lot. He always spends his time by playing with arrays.
Suddenly,He stuck with the hard problem. The problem is listed below.
Help, Ramesh to solve this magical problem of mathematics.
Given an array of integers, rearrange the array in such a way that the
first element is first maximum and second element is first minimum and so on.
You will be given a function which will take integer array as an argument.
Return the each element of the array after alternate sorting from the function in the form of integer array.
Sample Input :
7
1
2
3
4
5
6
7
Sample Output :
7
1
6
2
5
3
4
*/
import java.io.*;
import java.util.*;
class Main
{
public static void alter(int arr[])
{
int n = arr.length;
Arrays.sort(arr);
Set <Integer> x = new HashSet<>();
for(int i = 0 ; i < n; i++)
{
x.add(arr[i]);
}
arr = new int[x.size()];
int c = 0;
for(int p :x) {
arr[c++] = p;
// System.out.println(p);
}
int res[] = new int[arr.length];
Boolean flag = true;
int k = arr.length-1;
int g = 0;
for(int i = 0; i < arr.length; i++)
{
if(flag)
res[i] = arr[k--];
else
res[i] = arr[g++];
flag = !flag;
}
/*for(int o: res)
{
System.out.println(o);
}*/
return res;
}
public static void main (String[] args) throws java.lang.Exception
{
int arr[] = {7,1,2,3,4,5,6,7};
alter(arr);
}
}