Question: Hello, HackerRank deems this code to be too Slow can someone help me make it faster than n^2? without using Hash maps i'd

Hello,

HackerRank deems this code to be too "Slow" can someone help me make it faster than n^2?

without using Hash maps

i'd appreciate if someone can help me with this using binary search algorithm.

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; } } }

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; } } }

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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!