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:...
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.
Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones....
Find all rectangles filled with 0 We have one 2D array, filled with zeros and ones. We have to find the starting point and ending point of all rectangles filled with 0. It is given that rectangles are separated and do not touch each other however they can touch the boundary of the array.A rectangle might contain only one element. In Javascript preferred. Examples: input = [ [1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1,...
You are given an array of n elements, and you notice that some of them are...
You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n –...
Consider the following program specification: Input: An integer n > 0 and an array A[0..(n – 1)] of n integers. Output: The largest index b such that A[b] is the largest value in A[0..(n – 1)]. For example, if n = 10 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 1, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 4, since the largest value in A is 8 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT