Question
Hello, HackerRank deems this code to be too Slow can someone help me make it faster than n^2? The question is: You are
Hello,
HackerRank deems this code to be too "Slow" can someone help me make it faster than n^2?
The question is:
You are given a list of size n (input). You are also given a list of values in which you must print the number of times each value appears in the initial list.
my code which runs in n^2:
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) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { int inp = input.nextInt(); arr[i] = inp; } int temp, count = 0; while (input.hasNext()) { temp = input.nextInt(); for (int i = 0; i < arr.length; i++) { if (arr[i] == temp) count++; } System.out.println(count); count = 0; } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started