Question

public static int getv(int num, List<Integer> rating, int target) { int count 0; HashMap<Integer, Integer> map = new HashMap<

What is the time and space complexity of this code with explanation?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

In hashmap, inserting an element takes O(1) time,
getting an element from any index also takes O(1) time.

The first loop runs from 0 to num.
- map.conttainsKey takes O(1)
- rating.get(i) also takes O(1)
- map.put also takes O(1)
- map.put also takes O(1)

hence this loop takes O(num) to execute.


Second loop runs from o to num.
- map.get, rating.get() takes O(1)

So all the operations inside this loop execute in constant time(O(1))

Hence this loop also takes O(num) time.

so T(num) = O(num) + O(num)
T(num) = 2.O(num)

since O(big oh) represents the upper bound on the function,
hence we can neglect the constant terms.

T(num) = O(num)

Add a comment
Know the answer?
Add Answer to:
What is the time and space complexity of this code with explanation? public static int getv(int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA) I need to write an accessor method for the employeeDatabase field. The method is supposed...

    (JAVA) I need to write an accessor method for the employeeDatabase field. The method is supposed to be named getEmployeeDatabase() and have a return type of Hashmap<Integer,Employee>. I have also included the test case below that is being used for this code to see if it works properly private HashMap<Integer, Employee> employeeDatabase; public CompanyO f Scanner employeeDatabasepopulateEmployeeDatabase(keyboard) keyboard - new Scanner(System.in); public static void main(String[ args) private HashMap<Integer, Employee> populateEmployeeDatabase(Scanner keyboard) int numobjects -keyboard.nextIntO; String words HashMap <Integer, Employee mapnew...

  • Which big-O expression best characterizes the worst case time complexity of the following code? public static...

    Which big-O expression best characterizes the worst case time complexity of the following code? public static int foo(int N) ( int count = 0; int i1; while (i <N) C for (int j = 1; j < N; j=j+2) { count++ i=i+2; return count; A. O(log log N) B. O(log N2) C. O(N log N) D. O(N2)

  • COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;...

    COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) {        int bucketCount = array.length/2;        int minIntValue = 0;        int maxIntValue = array.length - 1;        // Create bucket array        List<Integer>[] buckets = new List[bucketCount];        // Associate a list with each index in the bucket array           for(int i = 0; i < bucketCount; i++){            buckets[i] = new LinkedList<>();        }        //...

  • Must comment on the code at every "/* Comment Here */" section about what the code...

    Must comment on the code at every "/* Comment Here */" section about what the code is doing. import java.io.*; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import java.util.Map; import java.util.HashMap; public class Configuration extends DefaultHandler { private Map map; private String configurationFile; /* Comment Here */ public Configuration(String configurationFile) throws ConfigurationException {    this.configurationFile = configurationFile;    map = new HashMap();    try {    // Use the default (non-validating) parser    SAXParserFactory factory = SAXParserFactory.newInstance();...

  • Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList;...

    Please Modify TestPart2 to test the correctness and efficiency of FasterDefaultList. Thanks import java.util.List; import java.util.AbstractList; import java.util.Map; import java.util.HashMap; public class DumbDefaultList<T> extends AbstractList<T> { Map<Integer,T> map; public DumbDefaultList() { map = new HashMap<Integer,T>(); } public int size() { return Integer.MAX_VALUE; } public T get(int i) { return map.get(i); } public T set(int i, T x) { return map.put(i, x); } public void add(int i, T x) { Map<Integer, T> map2 = new HashMap<Integer,T>(); for (Integer k : map.keySet())...

  • Convert Hex To Dec in java with HashMap and put in .txt file I'm making a...

    Convert Hex To Dec in java with HashMap and put in .txt file I'm making a compiler for assembler in JAVA. My program after several processes leaves a text file as follows. 02TLIGHT2.ASM file with this: CLO Start: MOV AL,0 OUT 01 MOV AL,FC OUT 01 JMP Start END Im trying by means of my hashmap convert the commands to his assigned hexadecimal value and put it in a text file? The output of my actual program in a 02TLIGHTHexa.ASM...

  • What will the code shown below print to the console? public class Cascade public static void...

    What will the code shown below print to the console? public class Cascade public static void print(int arg){ for(int i=0; i<arg; i++){ System.out.print(""); } System.out.println(""); if(arg > 1){ print(arg-1); } } public static void main(String[] args) { print(4); } } E B IV AA- IES XX, SE V GT 12pt Paragraph -

  • Using C++ please explain What is the Big-O time complexity of the following code: for (int...

    Using C++ please explain What is the Big-O time complexity of the following code: for (int i=0; i<N; i+=2) { ... constant time operations... Select one: o a. O(n^2) O b. O(log n) c. O(n) O d. 0(1) What is the Big-O time complexity of the following code: for(int i=1; i<N; i*=2) { ... constant time operations... Select one: O O a. O(n^2) b. 0(1) c. O(n) d. O(log n) O What is the Big-O time complexity of the following...

  • Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int...

    Java Do 61a, 61b, 61c, 61d. Show Output and Code. public class Employee { private int id; private String name; private int sal; public Employee(int id, String name, int sal) { super(); this.id = id; this.name = name; this.sal = sal; } public int getid) { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; public void setName(String name) { this.name = name; } public int get Sall) { return sal;...

  • 1. What is the output when you run printIn()? public static void main(String[] args) { if...

    1. What is the output when you run printIn()? public static void main(String[] args) { if (true) { int num = 1; if (num > 0) { num++; } } int num = 1; addOne(num); num = num - 1 System.out.println(num); } public void addOne(int num) { num = num + 1; } 2. When creating an array for primitive data types, the default values are: a. Numeric type b. Char type c. Boolean type d. String type e. Float...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT