-
Notifications
You must be signed in to change notification settings - Fork 0
/
Longest alternating subsequence .txt
48 lines (48 loc) · 1.13 KB
/
Longest alternating subsequence .txt
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
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
public static void main (String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuffer sb=new StringBuffer();
int T=Integer.parseInt(br.readLine());
while(T-->0)
{
int n=Integer.parseInt(br.readLine());
String[] s=br.readLine().split("\\s");
int[] arr=new int[n];
for(int i=0;i<n;i++)
{
arr[i]=Integer.parseInt(s[i]);
}
int ans1=1+las(arr,0,0);
int ans2=1+las(arr,0,1);
sb.append(Math.max(ans1,ans2)+"\n");
}
System.out.print(sb);
}
public static int las(int[] arr,int point,int gre)
{
if(point+1==arr.length)
{
return 0;
}
if(gre==1)
{
if(arr[point]<arr[point+1])
{
return 1+las(arr,point+1,0);
}
return las(arr,point+1,1);
}
else{
if(arr[point]>arr[point+1])
{
return 1+las(arr,point+1,1);
}
return las(arr,point+1,0);
}
}
}