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

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

Related

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

Algorithm to detect duplication of integers in an unsorted array of n integers. (implemented with 2 nested loops) [duplicate]

This question already has answers here:
Limit input data to achieve a better Big O complexity
(3 answers)
Closed 8 years ago.
You are given an unsorted array of n integers, and you would like to find if there are any duplicates in the array (i.e. any integer appearing more than once).
Describe an algorithm (implemented with two nested loops) to do this.
My description of the algorithm:
In step 1, we write a while loop to check to see if the array is empty/null, if the array is not null then we proceed with an inner loop.
Step 2, we now write a for loop to run an iteration of n-1 and in that loop we will assign to current (variable) the first index in the array (in the first iteration) and we will update the current variable by index + 1 each time through the iteration which means that the first time, current will hold the first index in the array and the second time, it will hold the second index in the array and so on until the loop ends.
Step 3, we will write a loop within the for loop (used in step 2) to compare the current number to all the integers in the array, if the integer equals to the next number then we will print the number using a printf statement else update next to hold the next index in the array and use that to compare to the current variable and do so until it has been compared to all the integers in the array and once this has been done, the current variable will be updated to store the next index of the array and will compare that particular number to all the integers in the array.
Will the algorithm be correct? (according to the question)... you're suggestions would be grateful. And no! it's not a homework question or such. Thank you for your time.
The complexity is definitely O (N^2) = N * ((N + 1)/2) Or O(N^2) in its simplified manner.
Edit:
I have added a description of an algorithm that is more efficient (in the question below). But going back to the question above, would it be suitable as an answer for an exam question? (it has shown up in previous papers so i would really appreciate your help).
If we limit the input data in order to achieve some best case scenario, how can you limit the input data to achieve a better Big O complexity? Describe an algorithm for handling this limited data to find if there are any duplicates. What is the Big O complexity?
If we limit the data to, let’s say, array size of 5 (n = 5), we could reduce the complexity to O(N). If the array is sorted, than all we need is a single loop to compare each element to the next element in the array and this will find if duplicates exist. Which simply means that if an array given to us is by default (or luckily) already sorted (from lowest to highest value) in this case the reduction will be from O(N^2) to O(N) as we wouldn’t need the inner loop for comparing the integers for sorting since it is already sorted therefore we could implement a single loop to compare the integers to its successor and if a duplicate is encountered, then we could, for instance, use a printf statement to print the duplicates and proceed to iterate the loop n-1 times (which would be 4)- ending the program once that has been done. The best case in this algorithm would be O(N) simply because the performance grows linearly and in direct proportion to the size of the input/ data so if we have a sorted array of size 50 (50 integers in the array) then the iteration would be n-1 (the loop will iterate 50 – 1 times) where n is the length of the array which is 50. The running time in this algorithm increases in direct proportion to the input size. This simply means that in a sorted array, the amount of time the operations take to perform is completely dependent on the input size of the array.
p.s. Sure there are other algorithms efficient and faster but from my knowledge and from what the question asks is for a better big o complexity in the first question and i believe this algorithm achieves that. (correct me if i'm wrong)- thanks :)
You describe three loops, but the first is actually just a condition (If is null or empty abort).
The remaining algo sounds good, except I'd say instead of "current will hold the first index in the array" (which nitpicks would insist is always 0 in C) "current will hold value of first element in the array" or such.
As an aside (although I understand it's a practice assignment) it's so terribly inefficient (I think n^2 is correct). I urge to just have one loop over the array, copying the checked numbers in a sorted structure of some kind and do binary searches in it. (As a teacher I'd have my students describe a balanced tree first so that they can use it here, like a virtual library ;-) ).

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.

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.

How to remove duplicates from an array [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Array remove duplicate elements
This is a homework question. I think the best way is a modified merge sort, where the modified merge just does not copy duplicate elements from the two input arrays to the result array. The complexity is O(N*logN) (exactly as the regular merge sort). Does it make sense ? Are there any better solutions ?
If you do not need to sort, only remove the duplicates it can be done in O(N) time by using a HashSet (or your language equivalent).
Sounds good. Or you can do any kind of sort and then remove duplicates with complexity O(N)...
First you must Sorted the array Since it is a sorted array any duplicates would be next to each other. So start at the beginning and compare each element to it neighbor on its right. If there are no duplicates then you have made n-2 comparisons and you are done.
If you find a duplicate you are going to have a hole. Since it is an array an not a link list you will need to move every element down. (You could create a new array if that was allowed.) However you do not just want to start moving everything down, you can simply mark with a pointer or counter where the hole is and move on to the next comparison. When you get the next non-duplicate you move(copy) that element into the hole and increment your hole counter by 1.

Resources