Question

In: Computer Science

Given an unsorted integer array A of size n, develop an pseudocode with time complexity as...

Given an unsorted integer array A of size n, develop an pseudocode with time complexity as low as possible to find two indexes i and j such that A[i] + A[j] = 100. Indexes i and j may or may not be the same value.

Solutions

Expert Solution

Given : unsorted array A[] of size n

Aim : Find two indexes i and j such that A[i] + A[j] = 100

Approach : This can be done using the concept of Hashing, because we want to reduce the time taken for lookup in an array.For Hashing this lookup time is O(1),that is constant time.(The notation used is BigO notation). Hashing is the concept of using a key to store a value using a dictionary. If you are using C++, this dictionary will be in the form of an unordered map. Here the value of a particular dictionary element will be the index value-i in the original array and the key used to store this value will be the A[i].

PseudoCode :

1) Take the input from the user and store the array in a variable named A,if not done so already.Also store the size of array in the variable N which is the size of the array.

2) Create an empty dictionary (unordered map for C++) named myDict.

3) Run a loop for i = 1 to N-1. For every iteration perform the following operation

if A[i] == 50 return {i,i}

else myDict[A[i]] = i (update the dictionary)

increment i by 1

4) Run a loop for i = 1 to N-1. For every iteration perform the following operation

if myDict[100 - A[i]] is not NULL then return {myDict[100 - A[i]] , myDict[A[i]] }

increment i by 1

5) If the function has not returned anything by now the return " No such pairs of i and j exist "

Analysis :

The time complexity of this solution is O(n). It cannot be improved beyond this.The first loop has runs N iteration,hence takes O(n) time.The second loop will also take O(n) time ,considering the worst case. Thus O(n) + O(n) = O(n).

NOTE

If the array has repetitve values, for example the value 56 appears at both index 4 and 10 ,myDict will store the largest value of array index which in this case is 10.This will be used for lookup in this algorithm  


Related Solutions

Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending...
Radix sortCome up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. I need this in java language.
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in...
Radix sort Come up with an unsorted array of numbers (integer array). Sort the numbers in ascending order and descending order and display them using radix sort. First sort in ascending, then reset the array to its original order and finally sort the array again in descending order. (Write a C# program)
The following pseudocode finds the maximum element in an array of size n. Int MAX (int...
The following pseudocode finds the maximum element in an array of size n. Int MAX (int A [ ], int n) { M = A[0]; for i = 1 to n – 1     if (A[i] > M)         M = A[i]        //Update the max return M; } Write a recursive version of this program. Let f(n) be the number of key comparisons performed by this algorithm. Write a recurrence equation for f(n). Prove by induction that the solution of...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions:...
Problem 1: Unsorted arrays Given an array of integers that is unsorted, implement the following functions: • myAdd ( ): add an integer d to the array; return 0 if the operation is successful; return a negative number otherwise. • search ( ): given an integer d, if d is found in the array, return the index of the cell containing d. Return a negative number otherwise (e.g., d is not found in the array). • myRemove ( ): Step...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n...
Given a positive integer k and an array A[1..n] that contains the quiz scores of n students in ascending order, design a divide and conquer algorithm to efficiently count the number of students that have quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. Let group i be the set of students with quiz scores in (100(i − 1)/k, 100i/k] for integers 1 ≤ i ≤ k. The counting result should be stored in G[1..k],...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array...
IN JAVA PLEASE Given an unsorted array numbers of integers with duplicate values. Sort the array and remove the duplicates in-place such that each element appears only once in the input array and returns the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Find the time complexity of your removeDuplicates() method in Big-O notation and write that in a comment line on the top...
Pseudocode and algorithm of finding median of unordered array in linear time.
Pseudocode and algorithm of finding median of unordered array in linear time.
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find...
Given an array of positive integers except one negative integer. Develop a divide-conquer algorithm to find the index of the negative integer, and compute its average complexity.
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements....
Implement a Priority Queue (PQ) using an UNSORTED LIST. Use an array size of 10 elements. Use a circular array: Next index after last index is 0. Add the new node to next available index in the array. When you add an element, add 1 to index (hit max index, go to index 0). Test if array in full before you add. When you remove an element, from the list, move the following elements to the left to fill in...
We have an array A of size n. There are only positive integers in this array....
We have an array A of size n. There are only positive integers in this array. Note that the array may have integers that are not distinct, and it could be any array of positive integers in creation (I like the numbers found the decimal expansion of π for instance). When possible provide the exact complexity of the algorithm. If it’s not possible explain the O/Ω/Θ complexity. a. Design an efficient algorithm to find the maximum difference between any two...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT