The input is an array of N integers ( sorted ) and an integer X. The algorithm returns true if X is in the array and false if X is not in the array. Describe an algorithm that solves the problem with a worst case of O(log N) time.
We will use the binary search approach here, which can solve this problem in O(log N) time.
#Algorithm
L=0 #left
R=Array.length-1 #right
while L<=R:
mid = (L+R)/2
if (a[mid] == target):
return true
if (a[mid] < target):
L = mid+1
else:
R = mid-1
return false
This is a divide and conquer methodology where you first take the middle element of the array and check if it is the target value, if the middle value is less than target value then you have to search on right sub array to the middle else you have to search on left subarray.
Example:
here we first search middle element which is 6, as the target is 10 which is greater than 6 so we check on right subarray.
Now we find the middle element in that sub-array which is 10, and we found out the target value.
As we are dividing the array into half at every single step.
array length divides like this.
Array sizes: N -> N/2 -> N/4 -> N/8 ....
which algorithmically gives a complexity of O(logN)
The input is an array of N integers ( sorted ) and an integer X. The...
Given as input an array A of n positive integers and another positive integer x, describe an O(nlogn)-time algorithm that determines whether or not there exist two elements Ai and AONn the array A such that is exactly x.
In Java: In a sorted (ascending) integer array of length n with no duplicates, print all values in the range x to y. Assume both x and y are in the array. What is the worst case big O running time if there are k integers within the range?
1. (16 pts.) Sorted Array Given a sorted array A of n (possibly negative) distinct integers, you want to find out whether there is an index i for which Al = i. Give a divide-and-conquer algorithm that runs in time O(log n). Provide only the main idea and the runtime analysis.
6. Let T(1..n] be a sorted array of distinct integers, some of which may be negative. Give an algorithm that can find an index i such that 1 <i<n and T[i] = i, provided such an index exists. Your algorithm should take a time in O(lg n) in the worst case. Answers must be proven (or at least well justified)
You are given an array A of integers in sorted order. However, you do not know the length n of the array. Assume that in our programming language arrays are implemented in such a way that you receive an out-of-bounds error message whenever you wish to access an element A[i] with i>n. For simplicity we assume that the error message simply returns the value INT_MAX and that every value in the array is smaller than INT_MAX. (a) Design an algorithm...
Consider the following problem: Input: a list of n-1 integers and these integers are in the range of 1 to n. There are no duplicates in list. One of the integers from 1 to n is missing in the list. Output: find the missing integer Let the input array be [2, 4, 1, 6, 3, 7, 8]. Elements in this list are in the range of 1 to 8. There are no duplicates, and 5 is missing. Your algorithm needs...
Given two arrays A and B of n integers both of which are sorted in ascending order. Write an algorithm to check whether or not A and B have an element in common. Find the worst case number of array element comparisons done by this algorithm as a function of n and its Big-O complexity
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.
Show that any array of integers x[1...n| can be sorted in O(n+M) time, where М = maxxminx; For small M, this is linear time: why doesn't the 2(nlogn) lower bound apply in this case?