-
Notifications
You must be signed in to change notification settings - Fork 694
/
Copy pathSolution.java
48 lines (42 loc) · 1.47 KB
/
Solution.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
//Problem: https://www.hackerrank.com/challenges/picking-numbers
//Java 8
/*
Initial Thoughts:
Use a hashmap to store all of our frequencies
as we read in the values, then iterate over
the map keeping a running max as we compare
the element 1 > and 1 < then the current
iteration
Time Complexity: O(n) //We have to look at every input integer
Space Complexity: O(n) //We have to store a map of at most all elements
*/
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) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Map<Integer,Integer> frequencies = new HashMap<>();
for(int i=0; i < n; i++){
int num = in.nextInt();
if(frequencies.containsKey(num))
frequencies.put(num, frequencies.get(num)+1);
else
frequencies.put(num, 1);
}
int maxSet = 0;
for(int i : frequencies.keySet())
{
int left = (frequencies.containsKey(i-1)) ? frequencies.get(i) + frequencies.get(i-1) : frequencies.get(i);
int right = (frequencies.containsKey(i+1)) ? frequencies.get(i) + frequencies.get(i+1) : frequencies.get(i);
if(left > maxSet)
maxSet = left;
if(right > maxSet)
maxSet = right;
}
System.out.println(maxSet);
}
}