Question

Given an unsorted integer array A of size n, develop a python version pseudocode with time...

Given an unsorted integer array A of size n, develop a python version 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.

Homework Answers

Answer #1


#Using hashing technique we can solve this problem efficiently

# Algorithm

# 1. Initialize an empty hash set s.
# 2. Do following for each element A[i] in A[]
# 1. If s[100 – A[i]] is set then print the pair (A[i], 100 – A[i])
# 2. Insert A[i] into s.


# Pseudocode

# s = set()
# for(i in range(len(A)))
# find(100-A[i])
# if(find(100-A[i]) in s):
# print(i,j as A.index(find(100-A[i]),i))
# s.add(A[i])

# Python implimentation
def printPairs(A,size):
# Create an empty hash set
s = set()
for i in range(0,size):
temp = 100-A[i]
if (temp in s):
print("two indexes i,j such that are: {} and {}".format(A.index(temp),i))
s.add(A[i])


# Test the implimented function
A = [1, 55, 45, 40, 10, 8]
printPairs(A,6)


# Time Complexity Analysis

# As we can see here array is travelled only once so the time complexity will be O(n)
# We need extra space of n elements which is hash set so the space complexity is also O(n)

Know the answer?
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
Similar Questions
Find the worst-case complexity of the algorithm below. Show your work. UFSizeCalc Input:  uf: Union-Find array of...
Find the worst-case complexity of the algorithm below. Show your work. UFSizeCalc Input:  uf: Union-Find array of size n Input: n: size of uf Output: size array for uf; that is, an array s such that s[r] equals the number of elements in the Union-Find tree rooted at r, for every root r (s may have any value for indexes that are not roots of uf) Pseudocode: For i = 1 to n uf.Find(i) size = Array(n) Initialize size to be...
Given a sorted array A with distinct values which has been rotated r times to the...
Given a sorted array A with distinct values which has been rotated r times to the left. Find r in least possible time. Provide python source code, time complexity, and pseudocode. Input: The sorted array ? that has been rotated r times to the left. Output: The value and ?.
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given N distinct integer points in 1-dimension. That is, all points are integers on the x-axis, and are in an array. This algorithm should go through all pairs (without sorting the elements of the array), one pair at a time, to find the current closest pair. For example, assume we had the following integers: 4, 2, -2, -1 in an array. We would then generate...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
We are given an array A of size n containing n positive and negative integers (the...
We are given an array A of size n containing n positive and negative integers (the array is indexed starting from 0). Our goal is to find two indices i and j such that 0 ≤ i ≤ j ≤ n and Pk=j k=i A[k] is maximized. Input sequence: [2, -4, 1, 9, -6, 7, -3] Output: [1, 9, -6, 7] or i = 2 and j = 5 Input sequence: [1, 2, 3, 4, 5, 6, -3] Output: [1,...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm...
How to measure the time complexity of an algorithm? Identify an important operation in the algorithm that is executed most frequently. Express the number of times it is executed as a function of N. Convert this expression into the Big-O notation. A. For each of the three fragments of code, what is its worst-case time complexity, in the form "O(…)". (Use the given solution to the first problem as a model)                 //----------------- This is a sample problem – solved ------...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list...
python 3 For this exercise you are to implement the function poly_iter in the array-backed list class, which, when called with positive integer parameters a, b, c, returns an iterator over the values in the underlying list at indexes a*i2+b*i+c, for i=0, 1, 2, ... E.g., given an array-backed list lst containing the elements [0, 1, 2, 3, 4, ..., 98, 99], the following code: for x in lst.poly_iter(2, 3, 4): print(x) will produce the output: 4 9 18 31...
In this programming exercise you will create an algorithm for solving the following version of the...
In this programming exercise you will create an algorithm for solving the following version of the m Smallest Numbers problem.   Instead of just returning the m smallest values as in homework 1, you want to return a list of the positions where the m smallest values are located without changing the original array. Your algorithm should meet the following specifications: mSmallest( L[1..n], m ) Pre: L is a list of distinct integer values. n is the number of elements in...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform...
Using C++, Python, or Java, write a program that: In this programming exercise you will perform an empirical analysis of the QuickSort algorithm to study the actual average case behavior and compare it to the mathematically predicted behavior. That is, you will write a program that counts the number of comparisons performed by QuickSort on an array of a given size. You will run the program on a large number of arrays of a certain size and determine the average...
Develop a python program to find the different solutions, i.e. zero crossings, of a polynomial function...
Develop a python program to find the different solutions, i.e. zero crossings, of a polynomial function using Newton-Raphson’s method over a given range. To do this we will develop three functions: the first should be a function to evaluate the polynomial at a given value of the independent variable; the second function would evaluate the derivative of the polynomial at a given value of the independent variable; finally, the third function would implement Newton-Raphson’s (NR) method to determine a zero...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT