Given an integer x and a list of integers, we have an algorithm to determine whether two of the numbers add up to x. How can using hash tables improve the performance of your algorithm? (Give an overview of the idea in ~ 100 – 150 words).
We can store each element each element ina Hash Table. Now, we know that the time taken to search an element in the Hash Table is
O(1)
So, now we just have to traverse the array arr. So, for the i th element, we need to find the value of
x - arr[i]
in the Hash Table. If the element is found, then such pair exists, other wise traverse the remaining elements in the array and check again for the presence of
x - arr[i]
If we have traversed the ectire array and no such pair is found, then no such pair exists.
This method is better because in this case, we don't need the array to be sorted. As the best sorting algorithm on average takes
time. So, this time is not applicable in the case of Hashtable. We just store each element in HashTable in one traversal. In the other tarversal, we just check for the presence of
x - arr[i]
But, in the x and y type method, the array needs to be sorted.
So,
Time Taken =
But in the method for HashTable, the
Time Taken =
Hence, the HashTable approach is better.
Given an integer x and a list of integers, we have an algorithm to determine whether...
Suppose that we have two unsorted lists of integers A and B. The lists are the same size, n. a) Write an algorithm that outputs how many integers occur in both lists. An integer occurs at most once in each given list. For example: if A = [1,2,5,7,13,19] and B = [2,9,13,14,19,22], then we can see that elements {2, 13, 19} occur in both lists, so the output will be 3. b) If the lists were sorted, say how we...
9. (5 points) Please describe an algorithm that takes as input a list of n integers and finds the number of negative integers in the list. 10. (5 points) Please devise an algorithm that finds all modes. (Recall that a list of integers is nondecreasing if each term of the list is at least as large as the preceding term.) 11. (5 points) Please find the least integer n such that f() is 0(3") for each of these functions f()...
Given as input an array A of n positive integers and another positive integer x, describe an O(nlogn)-time algorithm that determines whether or not there exist two elements Ai and AONn the array A such that is exactly x.
In this project, you will work on the algorithm (discussed in Module 1) to determine the length of the longest sub sequence of consecutive integers in an array You will implement the algorithm using Hash tables. You are provided with sample code (in C++ and Java) representing the linked list-based implementation of Hash tables (as an array of Linked Lists). You could go through the code to understand the implementation of a Hash table and the functions that can be...
You are given a set of integer numbers A = {a1, a2, ..., an}, where 1 ≤ ai ≤ m for all 1 ≤ i ≤ n and for a given positive integer m. Give an algorithm which determines whether you can represent a given positive integer k ≤ nm as a sum of some numbers from A, if each number from A can be used at most once. Your algorithm must have O(nk) time complexity.
Given an array A[1..n] of positive integers and given a number x, find if any two numbers in this array sum upto x. That is, are there i,j such that A[i]+A[j] = x? Give an O(nlogn) algorithm for this. Suppose now that A was already sorted, can you obtain O(n) algorithm?
From previous homework you are already familiar with the math function f defined on positive integers as f(x)=(3x+1)/2 if x is odd and f(x)=x/2 if x is even. Given any integer var, iteratively applying this function f allows you to produce a list of integers starting from var and ending with 1. For example, when var is 6, this list of integers is 6,3,5,8,4,2,1, which has a length of 7 because this list contains 7 integers (call this list the Collatz list for 6). Write a C or C++...
3b) [4 pts]+ As in the Little SearchEngine assignment, consider a hash table that stores frequencies (number of occurrences) of words in a set of documents. Words are the keys, and for each word, the associated value is an array list of (document name, frequency) pairs, in descending order of frequencies. Now suppose you are given a list of 50 words. You are asked to find all documents in the hash table in which one or more of these words...
1. We can determine how many digits a positive integer has by repeatedly dividing by 10 (without keeping the remainder) until the number is less than 10, consisting of only 1 digit. We add 1 to this value for each time we divided by 10. a) Describe the algorithm in a recursive way b) Implement this recursive algorithm in C++ and test it using a main function that calls this with the values of 15, 105 and 15105. (Hint: Remember...
Suppose we are given two sorted arrays (nondecreasing from index 1 to index n) X[1] · · · X[n] and Y [1] · · · Y [n] of integers. For simplicity, assume that n is a power of 2. Problem is to design an algorithm that determines if there is a number p in X and a number q in Y such that p + q is zero. If such numbers exist, the algorithm returns true; otherwise, it returns false....