GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — hackerrank-java/src/JavaDeQueue.java at master · anishLearnsToCode/hackerrank-java

Expand file tree

/

JavaDeQueue.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (44 loc) · 1.85 KB
/

JavaDeQueue.java

Copy path

File metadata and controls

53 lines (44 loc) · 1.85 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// https://www.hackerrank.com/challenges/java-dequeue
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class JavaDeQueue {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int length = SCANNER.nextInt();
int subArraySize = SCANNER.nextInt();
int[] array = getArray(length);
Map<Integer, Integer> frequencies = getElementsOfFirstSubArray(array, subArraySize);
int maxUniqueElements = frequencies.size();
for (int index = 0 ; index < array.length - subArraySize ; index++) {
remove(frequencies, array[index]);
frequencies.put(array[index + subArraySize],
frequencies.getOrDefault(array[index + subArraySize], 0) + 1);
maxUniqueElements = Math.max(maxUniqueElements, frequencies.size());
}
System.out.println(maxUniqueElements);
}
private static void remove(Map<Integer, Integer> frequencies, int element) {
if (frequencies.containsKey(element)) {
if (frequencies.get(element) <= 1) {
frequencies.remove(element);
} else {
frequencies.put(element, frequencies.get(element) - 1);
}
}
}
private static int[] getArray(int length) {
int[] array = new int[length];
for (int index = 0 ; index < array.length ; index++) {
array[index] = SCANNER.nextInt();
}
return array;
}
private static Map<Integer, Integer> getElementsOfFirstSubArray(int[] array, int subArraySize) {
Map<Integer, Integer> frequencies = new HashMap<>();
for (int index = 0 ; index < subArraySize ; index++) {
frequencies.put(array[index], frequencies.getOrDefault(array[index], 0) + 1);
}
return frequencies;
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.