finding the given element in the given array [duplicate] - c

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.

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

Sorted rotated integer array, search algorithm [duplicate]

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.

Arrays issue in C programming [duplicate]

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).

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).

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

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.

Resources