Question

In: Computer Science

Python - You are given a sorted (from smallest to largest) array A of n distinct...

Python - You are given a sorted (from smallest to largest) array A of n distinct integers which can be positive, negative or zero. Design the fastest algorithm you can for deciding if there is an index i such that A[i] = i.

Solutions

Expert Solution

# Returns index of x in arr if present, else -1

def findElementAlgo (arr, l, r):

# Check base case

if r >= l:

#finding mid element

mid = int(l + (r - l)/2)

#checking if a[i]==i

if arr[mid] == mid:

return mid

# If index is smaller than element at inde, then it can only

# be present in left side

elif arr[mid] > mid:

return findElementAlgo(arr, l, mid-1)

# Else the element can only be present in right side

else:

return findElementAlgo(arr, mid+1, r)

else:

# Element is not present in the array

return -1

# Test array

arr = [ 1,1,4,4]

# Function call

result = findElementAlgo(arr, 0, len(arr)-1)

if result != -1:

print ("A[i]=i exist : ",result)

else:

print ("A[i]=i does not exist")

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

In python please :) How to get the n th smallest even value of given array?...
In python please :) How to get the n th smallest even value of given array? (Use for loop for this problem. Do not use existing codes. Use your own codes). For example, in given list [11,23,58,31,56,22,43,12,65,19], if n is defined as 3. The program will print 56. (By using for loop and obtain the set of evens. By using another for loop, from the set of evens remove (n-1) observations and break the loop and find the minimum observation...
In Java Find the second largest and second smallest element in a given array. You can...
In Java Find the second largest and second smallest element in a given array. You can hardcode/declare the array in your program.
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We...
Given an array A[1..n], with distinct values and k with 1 ≤ k ≤ n. We want to return the k smallest element of A[1.....n], in non-decreasing order. For example: A = [5, 4, 6, 2, 10] and k = 4, the algorithm returns [2, 4, 5, 6]. There are at least the following four approaches: a. heapify A and then extract k elements one by one b. sort the array (e.g. using MergeSort or HeapSort) and then read the...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing...
5. Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm? Java Language....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order....
Suppose you are given an n-element array containing distinct integers that are listed in increasing order. Given a number k, describe a recursive algorithm to find two integers in A that sum to k, if such a pair exists. What is the running time of your algorithm?
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest...
Summary In this lab, you complete a prewritten Python program that computes the largest and smallest of three integer values. The three values are -50, 53, 78. Instructions Two variables named largestand smallest are assigned for you. Use these variables to store the largest and smallest of the three integer values. You must decide what other variables you will need and initialize them if appropriate. Write the rest of the program using assignment statements, if statements, or elifstatements as appropriate....
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:...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i)...
Given an array of n numbers, not necessarily in sorted order, we wish to find: (i) the range (max - min) and (ii) the interquartile range (IQR) of the numbers. IQR is defined as the difference between the number at the 25% percentile and the number at the 75% percentile. What is the exact number of comparisons needed for computing the range and why? Using any algorithm from the chapters of the textbook covered so far as a subroutine, give...
Sixty students were given a history exam. Their scores are shown below, sorted from smallest to...
Sixty students were given a history exam. Their scores are shown below, sorted from smallest to largest. 21 31 34 34 35 36 37 38 39 40 41 41 41 44 45 45 53 65 72 72 72 72 73 75 75 75 76 76 76 76 76 77 77 78 78 79 79 80 80 80 81 81 82 82 82 83 83 83 83 83 83 83 84 85 86 86 88 88 93 94 1) Compute the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT