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
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 ?.
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...
Let B[1...n] be an array of integers. To express that no integer occurs twice in the...
Let B[1...n] be an array of integers. To express that no integer occurs twice in the B We may write? (check all the answers that apply) a) forall i 1..n forall j in 1...n B[i] != B[j] b)for all i in 1...n forall j in 1...n, i != j => B[i] !=B[j] c)forll i in 1...n forall j in 1...n i != j and B[i] != B[j] d)forall i in 1...n forall j in 1...n B[i] =B[j] => i=j e)forall...
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...
Given an array, A, of n−2 unique integers in the range from 1 to n, describe...
Given an array, A, of n−2 unique integers in the range from 1 to n, describe an O(n)-time method for finding the two integers in the range from 1 to n that are not in A. You may use only O(1) space in addition to the space used by A.
Let A m×n be a given matrix with m > n. If the time taken to...
Let A m×n be a given matrix with m > n. If the time taken to compute the determinant of a square matrix of size j is j to the power 3, find upper bound on the a) total time taken to find the rank of A using determinants b) number of additions and multiplications required to determine the rank using the elimination procedure.
C program question: Write a small C program connect.c that: 1. Initializes an array id of...
C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and changing all the entries with the same name as p to...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
you are given a list of n bits {x1,x2,...,xn} with each xi being an element in...
you are given a list of n bits {x1,x2,...,xn} with each xi being an element in {0,1}. you have to output either: a) a natural number k such that xk =1 or b) 0 if all bits are equal to zero. the only operation you are allowed to access the inputs is a function I(i,j) defined as: I(i,j) = { 1 (if some bit in xi, xi+1,...,xj has vaue 1), or 0, (if all bits xi,xi+1,...,xj have value 0}. the...
In python Please The general elections are over, now is the time to count the votes...
In python Please The general elections are over, now is the time to count the votes and find out who will take the reins of this country. There are c (2 <= c <= 6) candidates and m (1 <= m <= 74) voting regions. Given the number of votes for each candidate in each municipality, determine which candidate is the winner (the one with the most votes). Input Format The first line of input contains an integer T (1...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Suppose that people's heights (in centimeters) are normally distributed, with a mean of 170 and a...
    asked 3 minutes ago
  • Use the information from the following Income Statement to create and Projected Income Statement and solve...
    asked 16 minutes ago
  • An unequal tangent vertical curve has the following elements: g1=-3.25%, g2=75%, total length = 500.00’, length...
    asked 18 minutes ago
  • Please write clear definitions of the following legal terms. Commerce Clause Supremacy Clause Indictment Tort
    asked 23 minutes ago
  • Do you think Moralistic Therapeutic Deism is an accurate reflection of society today? What are relevant...
    asked 27 minutes ago
  • The mean operating cost of a 737 airplane is $2,071 per day. Suppose you take a...
    asked 36 minutes ago
  • Arguments can be made on both sides of this debate about the ethical implications of using...
    asked 42 minutes ago
  • In the Chapter, they mention the idea of strategizing around your cash flows. Why are cash...
    asked 47 minutes ago
  • Company A signed a fixed-price $6,500,000 contract to construct a building. At the end of Year...
    asked 48 minutes ago
  • An unequal tangent vertical curve has the following elements: g1=-3.25%, g2=1.75%, total length = 500.00’, length...
    asked 55 minutes ago
  • In a previous​ year, 61​% of females aged 15 and older lived alone. A sociologist tests...
    asked 1 hour ago
  • Topic: Construction - Subsurface Investigation (Note: Briefly discuss in your own words, 1 paragraph minimum.) Typically...
    asked 1 hour ago