forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLarrysArray.java
More file actions
76 lines (73 loc) · 2.48 KB
/
LarrysArray.java
File metadata and controls
76 lines (73 loc) · 2.48 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
75
76
package Implementation;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
* @author -- rajatgoyal715
*/
public class LarrysArray {
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=Integer.parseInt(br.readLine());
while(t--!=0){
int n=Integer.parseInt(br.readLine());
String s[]=br.readLine().split(" ");
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
int prev;
boolean flag1=false;
int count,temp;
label:
for(int j=0;j<n;j++){
count=0;
prev=a[0];
for(int i=1;i<n;i++){
if(a[i]<prev){
if(i+1<n){
if(a[i+1]<a[i]){
// 3 2 1 -> 1 3 2
temp=a[i+1];
a[i+1]=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
else if(a[i+1]<prev){
//3 1 2 -> 1 2 3
temp=a[i-1];
a[i-1]=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
else{
//2 1 3 -> 1 3 2
temp=a[i-1];
a[i-1]=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
else{
if(a[i-2]>a[i]){
//2 3 1 -> 1 2 3
temp=a[i];
a[i]=a[i-1];
a[i-1]=a[i-2];
a[i-2]=temp;
}
}
}
else{
count+=1;
}
prev=a[i];
}
if(count==n-1){
flag1=true;
break;
}
}
System.out.println((flag1)?"YES":"NO");
}
}
}