Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

What is coaching? Is there one type of coaching? Explain.

Answered: 1 week ago

Question

How to solve maths problems with examples

Answered: 1 week ago