I have written a solution to Memorise me problem on Hacker Earth but it takes a lot of memory space and time limit exceeds in the last test case.
The first line of input will contain \$N\$, an integer, which is the total number of numbers shown to your team.
The second line of input contains \$N\$ space separated integers.
The third line of input contains an integer \$Q\$, denoting the total number of integers.
The Next \$Q\$ lines will contain an integer denoting an integer, \$Bi\$, for which you have to print the number of occurrences of that number (\$Bi\$) in those \$N\$ numbers on a new line.
If the number \$Bi\$ isn’t present then print NOT PRESENT on a new line.
Constraints
\$1 \le N \le 105, 0 \le Bi \le 1000, 1 \le Q \le 1055\$
This is my code :
public static void main(String args[]) throws Exception {
// BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String noOfElements = br.readLine();
int n = Integer.parseInt(noOfElements);
int i = 0;
String[] numberArray = br.readLine().split(" ");
String noOfQueries = br.readLine();
int q = Integer.parseInt(noOfQueries);
String[] queries = new String[q];
for (i = 0; i < q; i++) {
String query = br.readLine();
String queryNumber = query.trim();
queries[i] = queryNumber;
}
for (i = 0; i < queries.length; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (queries[i].equals(numberArray[j])) {
count++;
}
}
if (count == 0) {
System.out.println("NOT PRESENT");
} else {
System.out.println(count);
}
}
}