-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRearrange Sequence - 1.java
More file actions
115 lines (58 loc) · 1.98 KB
/
Rearrange Sequence - 1.java
File metadata and controls
115 lines (58 loc) · 1.98 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Rearrange Sequence - 1
// You are given an array of size N containing unique integers. Find the size of the largest subarray that can be rearranged to form a contiguous sequence.
// A contiguous sequence means that the difference of adjacent elements should be 1.
// Input Format
// The first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.
// Output Format
// For each test case, print the size of the largest subarray that can be rearranged to form a contiguous sequence, on a new line.
// Constraints
// 30 points
// 1 <= T <= 100
// 4 <= N <= 100
// 70 points
// 1 <= T <= 100
// 4 <= N <= 1000
// General Constraints
// 0 <= A[i] <= 105
// Example
// Input
// 2
// 5
// 1 3 2 6 5
// 9
// 0 8 6 5 7 10 3 2 1
// Output
// 3
// 4
// Explanation
// Test-Case 1
// The largest subarray that can be rearranged to form a contiguous sequence is [1, 3, 2] which can be rearranged to form [1, 2, 3].
// Test-Case 2
// The largest subarray that can be rearranged to form a contiguous sequence is [8, 6, 5, 7] which can be rearranged to form [5, 6, 7, 8].
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t-->0){
int n=s.nextInt();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.nextInt();
}
int ans=0;
for(int i=0;i<n;i++){
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
for(int j=i;j<n;j++){
min=Math.min(min,a[j]);
max=Math.max(max,a[j]);
if(max-min==j-i)
if(ans<max-min+1) ans=max-min+1;
}
}
System.out.println(ans);
}
}
}