Skip to content

Instantly share code, notes, and snippets.

@vinnyoodles
Last active January 25, 2021 17:01
Show Gist options
  • Save vinnyoodles/b0c39d6f74a768445370f6141b7ab802 to your computer and use it in GitHub Desktop.
Save vinnyoodles/b0c39d6f74a768445370f6141b7ab802 to your computer and use it in GitHub Desktop.
public int findMax(int n, int[] arr) {
// let freqMap be a map where the keys are the elements in arr
// and the values are its frequency or the number of occurences in arr
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= n; i++) {
if (!freqMap.contains(i)) continue;
for (int count = 0; count < freqMap.get(i); count++) {
list.add(i);
}
}
while (list.size() < n) {
list.add(n);
}
list.set(0, 1);
for (int i = 0; i < n - 1; i++) {
if (list.get(i + 1) - list.get(i) > 1)
list.set(i + 1, list.get(i) + 1);
}
return list.get(n - 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment