finding an index i such that a[i] = i [duplicate] - c

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Interview question - Search in sorted array X for index i such that X[i] = i
You are given with a sorted array of integer and the length of array. Now you have to find an index i such that a(i)=i.
I am able to do it in o(logn) if index is given but what about if index i is not mentioned?

As Alexandre stated in a comment, the foreknowledge of the index means it's O(1), not O(log N).
And, unless there's some information you're not telling us, you need O(n) time to do this without that foreknowledge:
for x = 0 to len(a) - 1:
if a[x] = x:
return x
Clarification: The original question did not state that the list was sorted, that was added later. Since that makes the question a duplicate of this one, you should look to the answers there for the solution.
This answer will be left as is since there's no point in duplicating the others, and it's relevant for the unsorted case.

With the information given, you need to check values until you find a match. The worst case scenario (no match, or match found in last cell) is O(n).
If the array is already sorted you can do a binary search, which is O(log n).
Your claim is either false, or you left out some information.

Related

Swapping index and value in-place in an array [duplicate]

This question already has answers here:
How to invert a permutation represented by an array in-place
(3 answers)
Closed 8 months ago.
Let's say I have an array of N elements, each element representing a unique integer between 0 and N-1. Let's say I want to find the index of a given integer, I can loop through the array and return the index when I find the number. Since this is inefficient, I could make a second array, so that for first_array[10] = 55 we'd obtain second_array[55] = 10.
Now let's say that I don't want to create a second array and I want to do the switch in-place. Is there a clever way to do this, clever meaning no extra allocation, no crazy deep recursion and no searching through the whole array N times? Does this kind of operation have a name? I feel like this is a problem that must have already been solved but I can't imagine what it could be called.
Edit: This is a question about an algorithm, the answer, being an algorithm, should work the same regardless of the programming language.
if the question is about javascript, you can simply use indexOf() method.
in your case:
first_array.indexOf(55); // it will return index of **55** in first_array

Find the element on i th index without sorting and in O(n) complexity [duplicate]

This question already has answers here:
How to find the kth largest element in an unsorted array of length n in O(n)?
(32 answers)
Closed 7 years ago.
Recently I came across a question,
There is an unsorted array of n elements. Once we sort the array,
i th index will have an element. How would you find which element is going to be present on i th index in O(n) complexity on the unsorted array?
I tried many methods, finally I came to a conclusion that we may need to use an hash map. But later I found that hash map implementation usually follow a tree structure which has an log n complexity in insertion.
How shall I proceed?
You need Linear Time Selection algorithm. Its running time is O(n) in the worst case. You can find its description in chapter "9.3 Selection in worst-case linear time" of Introduction to Algorithms, Second Edition or on the Internet e.g. here.
You can also use Randomized select algorithm. It has the expected linear time. You can find its description in chapter "9.2 Selection in expected linear time" of the same book.

finding the given element in the given array [duplicate]

This question already has answers here:
Searching in an array for numbers with each element +1 or -1 of preceding element [closed]
(3 answers)
Closed 8 years ago.
I have an array of integers in which the difference of consecutive elements is only one
|arr[i]-arr[j]|=1, where |i-j|=1
without using linear search.
lets say suppose our array is
arr[]=5,4,3,2,1,0,-1,-2,-1,0,1,2,1;
We have to find out the index of the first occurrance of the given value say, -1
So,our output is 6.
What could be our approach to solve this problem?
I have tried by taking the difference between the given number and the current element and move ahead with that.
But I think I am wrong because its also called linear search. Please help me out.
cand <- 0
repeat:
if arr[cand] == x:
done
else:
cand <- cand + |arr[cand]-x|
The idea is - you can 'skip' every elements in between cand to cand + |arr[cand]-x| - the element cannot be there!
I think your method is OK because a linear search means you traverse every element once until you find the answer but you don't do that.

Given two arrays of distinct integers A and B of size m and n, what is the lower-bound complexity for finding one common element? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I have the following problem:
Given two arrays of distinct integers A and B of size m and n, what is the upper-bound complexity for finding the first common element? I can use at most O(k) memory in addition to the input arrays.
k is a constant (means that i can use only constant memory in addition to the input)
I've already asked this in another site:
https://cs.stackexchange.com/questions/12182/lower-bound-complexities-for-finding-common-elements-between-two-unsorted-arrays
but didn't solved the problem
I'm going to have a crack at the second problem in your original post on CS because I'm not sure your summarization of the problem is the best one.
Given two unsorted arrays of distinct integers, A and B, of size m and n; knowing that common elements between the arrays have the same relative order in both arrays and that for every couple of consecutive common elements, their distance is at most k(constant), determine all the common elements between the two arrays maintaining their relative order and using at most O(k) memory in addition to the input arrays.
I'll again use a heap as mentioned in the comments and in your edit on the original question, but this time we need only one heap.
Notice that in order to determine whether element A[i] has a match in array B, we need only check elements B[i-k]..B[i+k]. Construct a heap B' of these elements in O(k) time and 2k+1 = O(k) space using heapify on the subarray of B. Determining whether there exists a match for A[i] in B' takes O(log k). We then iterate through A for i=1..m, updating B' and checking for a match to A[i]. This maintains the relative order in the output. To determine whether there are matches for all m elements in A takes O(km log k), or O(m) time.
Notes:
Obviously you should iterate through the smaller array, putting the elements of the larger array into the heap. I've assumed throughout that m <= n.
You should never have to put more than k+1 elements into the heap at a time, which will happen when checking the first element of A. The heap can potentially grow to be up to 2k+1 elements while checking some element A[i], k < i < m-k. Outside of this range, the heap will always be smaller.
Once i is in the range k < i < m-k, if there is no match on the current index you will only have to add one element and delete one element from the heap to check the next index.
If there is a match, say at A[i] and B[j], you still have to add at most one element, but you can remove all of the elements from the heap with index less than or equal to j (since we know that elements have the same relative order, so A[i+1] will not match anything before B[j+1]). At some point it may become more efficient to simply rebuild the heap with elements B[j+1]..B[i+k].
Once our heap has grown to its maximum size of 2k+1, we are always removing at least as many elements as we are adding. Therefore our heap will never be larger than 2k+1.

determine whether there are 2 elements in unsorted array that sum up to Z in O(n) average time [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Determine whether or not there exist two elements in Set S whose sum is exactly x - correct solution?
Consider an unsorted array of numbers and an constant Z. We want to find whether there are two elements in the array whose sum is Z.
I know there is an O(n*lgn) algorithm to do so. But is there an algorithm that run in O(n) average case?
Well, after some thinking, I come up with the following scheme:
make a hash table of size 2n
for each value s in S, hash both s and Z-s into the hash table
if there is a collision, then there exists 2 element whose sum is equal to Z
this scheme has a running time of O(n).

Resources