Question

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.

Homework Answers

Answer #1

Hi
Please find the working Python code for the given problem, pass the list that contains the n-2 integers and this function will return the list of missing intergers.
Time Complexity = O(n+n) = O(n)
Space Complexity = O[1] as we have used the same list to do our operation, just appended two more values to the list i.e O[1]


PLEASE UPVOTE IF YOU LIKE THE ANSWER

Thanks

def findMissingIntegers(A):
   A += [0,0]
   n = len(A)
   itr = 0
   while(itr < n):
       idx = A[itr] - 1
       #need to swap idx and itr values
       tmp = A[itr]
       A[itr] = A[idx]
       A[idx] = tmp
       itr += 1
#after running the above loop all values wiill come to its respective place   
   missingIntegers = []
   for i,a in enumerate(A):
       if a == 0:
           missingIntegers.append(i+1)
   return missingIntegers

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
1. You are given an array of integers, where different integers may have different number of...
1. You are given an array of integers, where different integers may have different number of digits, but the total number of digits over all the integers in the array is n. Show how to sort the array in O(n) time. 2. You are given an array of strings, where different strings may have different numbers of characters, but the total number of characters over all the strings is n. Show how to sort the strings in O(n) time. (Note...
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...
Suppose an array A stores n integers, each of which is in {0, 1, 2, ...,...
Suppose an array A stores n integers, each of which is in {0, 1, 2, ..., 99}. Which of the following sorting algorithms can sort A in O(n) time in the worst case? Question 16 options: A) merge sort B) counting sort C) quicksort D) None of these options is correct. E) insertion sort
C++ program for : 1. Given an array of real numbers and the dimension n (n...
C++ program for : 1. Given an array of real numbers and the dimension n (n is entered from the keyboard). B form an array, each element bi - arithmetic mean of the array elements and excluding ai. 2. Given an array of integers dimension n. Change the array so that all the elements of the module which is not equal to the maximum element of the array, replaced by zero, and equal - unit. 3. Given an array of...
Write recursive method to return true if a given array of integers, named numbers, with n...
Write recursive method to return true if a given array of integers, named numbers, with n occupied positions is sorted in ascending (increasing) order, or returns false otherwise. Array can be empty or not. //PRECONDITION: Varible n denotes the number of occupied positions in the array and must be non-negative. Employee class has method getSalary() that returns employee's salary. // An empty array and an array with single element in it, are sorted. Method isSortedRec must be recursive and returns...
IN MATLAB: Suppose you are given an array with integers between 1 and 1,000. One integer...
IN MATLAB: Suppose you are given an array with integers between 1 and 1,000. One integer is in the array twice. How can you determine which one?
Given T(n)= T(n-1) + 2*n, using the substitution method prove that its big O for T(n)...
Given T(n)= T(n-1) + 2*n, using the substitution method prove that its big O for T(n) is O(n^2). 1. You must provide full proof. 2. Determine the value or the range of C.
Show that for all positive integers n ∑(from i=0 to n) 2^i=2^(n+1)−1 please use induction only
Show that for all positive integers n ∑(from i=0 to n) 2^i=2^(n+1)−1 please use induction only
Prove Summation of integers from 1 to n is n(n-1)/2
Prove Summation of integers from 1 to n is n(n-1)/2
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT