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

Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds...
Given an unsorted array A of size N of integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax. (no [ ]'s or (ptr+1) stuff. input will be the number of input values to enter followed by the sum to compare with. print out the continuous sub-array of values that are equal to sum or the message 'no sum ofund.' there may be more than one sub-array to be found in...
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)
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 assignment (CISP430 ignore this part) This is only for those who are using Java How many classes do I need? A: Node, Queue, Radix, Driver...
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. C++ program thanks
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...
assume n = 2^100. consider the following problem. given an unsorted list of size n you...
assume n = 2^100. consider the following problem. given an unsorted list of size n you can choose to sort it or not first. after this step, you get m queries q1, q2, ... qm. one by one of the form "is qi in the list?" Describe whether you would sort and what algorithm you would use if 1.) m= 10, and 2) m = 1000
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],...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT