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? 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

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

AWS Certified Database Study Guide Specialty DBS-C01 Exam

Authors: Matheus Arrais, Rene Martinez Bravet, Leonardo Ciccone, Angie Nobre Cocharero, Erika Kurauchi, Hugo Rozestraten

1st Edition

1119778956, 978-1119778950

More Books

Students also viewed these Databases questions