forked from namishkhanna/hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPickingNumbers.java
More file actions
35 lines (27 loc) · 928 Bytes
/
Copy pathPickingNumbers.java
File metadata and controls
35 lines (27 loc) · 928 Bytes
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
import java.util.*;
// A program that returns length of longest subsequence such that absolute difference between two numbers is <=1.
class Result {
public static int pickingNumbers(ArrayList<Integer> a) {
int[] freq = new int[100];
int max = 0;
for(int i=0;i<a.size();i++){
freq[a.get(i)] = freq[a.get(i)] +1;
}
for(int i=0;i<a.size()-1;i++){
if((freq[i]+freq[i+1])>max)
max = freq[i]+freq[i+1];
}
return max;
}
}
public class PickingNumber{
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> a = new ArrayList<Integer>();
for(int i=0;i<n;i++)
a.add(sc.nextInt());
int result = Result.pickingNumbers(a);
System.out.println("Length of longest subarray is: "+result);
}
}