This question already has answers here:
Searching a number in a rotated sorted Array
(20 answers)
Closed 7 years ago.
An integer sorted array rotated to left by unknown no of times, write an efficient algorithm to search for an element.
Example: 4 5 6 7 8 9 1 2 3 4
I am thinking every time I find a mid in Binary Search I compare the element with extreme end element and take a decision on which half to pick to repeat the process. Is it wrong? Or is there any efficient algo?
Your example array contains duplicates. When there are duplicates there is no efficient algorithm - you must always do O(n) work in the worst case.
To prove this, consider arrays of this form:
000000000000000010000000
It is a rotation of a sorted array, but in the worst cast you must iterate over every element to see where the 1 is.
Related
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
This question already has answers here:
Finding Maximum Value in an array
(3 answers)
Closed 7 years ago.
Ten million elements are entered into an array (no memory constraints). As we know, while entering the elements we can update the max out of entered values by a check whenever we enter a value.
But imagine if the position of max value is somewhere around 9 million
If I remove 2 million elements in positions 8 to 10 million
without doing any more comparisons, we should have the next maximum value.
Will that mean while entering the data we should have a plan to organize the data in some way to get the max value out of the remaining data?
Deleting and inserting will keep on happening, but we should have the new/residual maximum value updated in less time with a smaller number of steps. (Using multiple stacks might help.)
For that you can also do by inserting the values in the array in the sorted order,means checking the right place of the inserted element in the sorted form.
If block deletions are a common operation, you could maintain a hierarchy of maxima of spans of the list. For each edit you then have to update that data, but that's something like O(log n) rather than O(m log n) if you simply iterated through the list of m deletions removing them one-by-one from the heap.
This question already has answers here:
keeping track of the original indices of an array after sorting in C
(4 answers)
Closed 8 years ago.
Given an index of an element in the unsorted array, how do we find the index of the element in the sorted array?
I will explain the problem with an example. 5,4,1,2,3 was my unsorted array. I am given an index. Lets take the 3rd index, the value at 3rd index is 2. Now I have to find the index of value 2 after the array has been sorted. Please write sample code. Thank you :)
Assuming that all elements in the array are unique, scan through the array and count the number of items that would sort before the specified element. If there can be other elements that are "equal" to the specified element, then its position in a sorted array depends on the details of the sorting algorithm used (whether it is stable or not, and how it works if unstable).
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.
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).