-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathLarry'sArray.java
86 lines (84 loc) · 2.89 KB
/
Larry'sArray.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
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
77
78
79
80
81
82
83
84
85
86
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
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];
int a1,a2,a3;
for(int i=0;i<n;i++)
a[i]=Integer.parseInt(s[i]);
/*for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
*/
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 if(a[i-2]<a[i]){
flag1=false;
break label;
}*/
}
}
else{
count+=1;
}
prev=a[i];
}
/*for(int i=0;i<n;i++)
System.out.print(a[i]+" ");
System.out.println();
*/
if(count==n-1){
flag1=true;
break;
}
}
System.out.println((flag1)?"YES":"NO");
}
}
}