Question

In: Computer Science

You are given an array of length n that is filled with two symbols (say 0...

You are given an array of length n that is filled with two symbols (say 0 and 1); all m copies of one symbol appear first, at the beginning of the array, followed by all n − m copies of the other symbol. You are to find the index of the first copy of the second symbol in time O(logm).

include: code,  proof of correctness, and runtime analysis  

Solutions

Expert Solution

For this i have used the binary search feature to find the first index of second symbol

// Code

def code(arr,symbol1,symbol2):
n=len(arr)
left=0
right=n-1
while(left<right):
mid=int((left+right)/2)
if arr[mid]==symbol1:
left=mid+1
else:
right=mid
return right+1

arr=[0,0,0,0,0,1,1,1,1,1]
print("first index of second symbol :",code(arr,arr[0],arr[len(arr)-1]))

arr=[0,0,0,0,0,1]
print("first index of second symbol :",code(arr,arr[0],arr[len(arr)-1]))

arr=[0,0,1,1,1,1,1]
print("first index of second symbol :",code(arr,arr[0],arr[len(arr)-1]))

//OUTPUT

  • input

first index of second symbol : 6                                                                                                                                             

first index of second symbol : 6                                                                                                                                             

first index of second symbol : 3                                                                                                                                             

                                                                                                                                                                             

                                                                                                                                                                             

...Program finished with exit code 0                                                                                                                                         

Press ENTER to exit console.              

Code is being verified for various inputs

// RUNTIME ANALYSIS

at iteration 1 : length of array =n

at iteration 2 : length of array =n/2

at iteration 3 : length of array =n/4

at iteration 4 : length of array =n/8

at iteration k : length of array =

Applying log function on both sides:

 log2 (n) = log2 (2k)
 log2 (n) = k log2 (2)
As (loga (a) = 1)

Therefore,

 k = log2 (n)

HENCE THE COMPLEXITY IS : O(log2 (n))

//CODE

//OUTPUT

  


Related Solutions

Write a C++ program that has a function which given n>=0, create an array length n*n...
Write a C++ program that has a function which given n>=0, create an array length n*n with the following pattern, shown here for n=3 : {0, 0, 1, 0, 2, 1, 3, 2, 1} (spaces added to show the 3 groups) generateGroups(3) → [0, 0, 1, 0, 2, 1, 3, 2, 1] generateGroups(2) → [0, 1, 2, 1] generateGroups(4) → [0, 0, 0, 1, 0, 0, 2, 1, 0, 3, 2, 1, 4, 3, 2, 1]
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such...
Denition: An orthogonal array OA(k, n) on n symbols is an n2 x k array such that, in any two columns, each ordered pair of symbols occurs exactly once. Prove that there exists an OA(k, n) if and only if there exist (k - 2) mutually orthogonal Latin squares of order n. (combinatorics and design)
Given an array A of n distinct real numbers, we say that a pair of numbers...
Given an array A of n distinct real numbers, we say that a pair of numbers i, j ∈ {0, . . . , n−1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Answer the following: (a) How small can the number of inversions be? Give an example of an array of length n with the smallest possible number of inversions. (b)...
Given an array A of n distinct numbers, we say that a pair of numbers i,...
Given an array A of n distinct numbers, we say that a pair of numbers i, j ∈ {0, . . . , n − 1} form an inversion of A if i < j and A[i] > A[j]. Let inv(A) = {(i, j) | i < j and A[i] > A[j]}. Define the Inversion problem as follows: • Input: an array A consisting of distinct numbers. • Output: the number of inversions of A, i.e. |inv(A)|. Answer the following:...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Using Java, Given an array A[0 ... n-1], where each element of the array represent a...
Using Java, Given an array A[0 ... n-1], where each element of the array represent a vote in the election. Assume that each vote is given as an integer representing the ID of the chosen candidate. Can you determine who wins the election? What is the complexity of your solution? Hint: it is similar to finding the element that is repeated the maximum number of times.
Let's say someone gives you an array that is filled with P numbers. Please next see...
Let's say someone gives you an array that is filled with P numbers. Please next see if there are two numbers whose sum equals a given number S and determine if there is two numbers that do this. Take for example, if I give you the input array to be 8, 2, 1, 7, and our variable S is 15, then the answer would be yes because 8 and 7 add up to S which in this case is 15....
Create a program that has an array of length 100 which will automatically be filled with...
Create a program that has an array of length 100 which will automatically be filled with randomly generated numbers between 1 and 100 each time the program is run. Have the program ask the user to enter a number between 1 and 100. Check to see if that number is one of the values in the array. If it is display “We found your number XX at position YY in the array” If the number is not in the array...
Given an array A[1..n] of integers - all of whose numbers are in the range [0,...
Given an array A[1..n] of integers - all of whose numbers are in the range [0, n^3 − 1] give an algorithm which sorts them in O(n) time.
//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for...
//2.-------------------------------------------------------------- Given an array of size N, what is the complexity? int m = A[0]; for (int i=1; i<N; i++) { if (A[i] > m) m = A[i]; } int k = 0; for (int i=0; i<N; i++) { if (A[i] == m) k++; } What is a (name one) most frequent operation performed?______________________ Expression showing the number of times it is performed ___________ Time Complexity in Big-O Notation O( ) _____________________________________________________ //3.-------------------------------------------------------------- int mult(int N, int M) {   ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT