Checking two integer arrays if second array contains bigger elements than each of the element of first array in C
a[10]={34,234,3,4,5646}
b[10]={5849,39,1,7,45}
How can I find out that B array contains greater than or equal element than each of the elements of A array? Is there any particular algorithm?
From what I can understand you can find the largest element of array a in O(n) and find the smallest element of array b in O(n) and compare them
Related
I am looking for an algorithm that returns the indice of the kth largest element in a array. I found many algoritms but most of them return the list of the k largest elements (Extract K largest elements from array of N integers in O(N + K) time, Best way to retrieve K largest elements from large unsorted arrays?, ...).
In this case, only the indice of the kth largest element is needed. All the kth largest elementS are not needed. As array and k are large, I would like to avoid the allocation of an array (or other structure, e.g. linked list) of dimension k and the initial array must be unchanged. What is (are) the most efficient algorithm(s) ?
Finding the k'th largest element in an array cannot be done in less than O(k*n) (or O((n-k)*n)) time without modifying the input array or allocating more than O(1) additional space. If you do not permute the array, you can't do any better than brute force; if you do permute the array, you can't reverse the permutation without keeping extra information around to do it.
(A randomized selection algorithm can achieve linearithmic expected time, but cannot improve on the worst-case time.)
I've been trying to figure out how to implement the following algorithm for a K way merge.
Algorithm:
1)Initialize an array of size n*k.
2) Initialize a min heap of size k, to hold the smallest element of each array.
3) Add the smallest element from the minHeap into the output array.
4)Move the next element from the array from which the min element was derived onto the heap. // How would one implement this?
5) Repeat step 4 until all the arrays are empty and the minHeap is empty.
I've been able to implement all but Step 4 of my algorithm. How would one track the array from which the smallest element has been extracted?
Try keeping element and array in pair. Element would be key, array (pointer to it) would be value. Pair should be sortable by it's keys.
When you extract the smallest pair from the heap, you take the key from the pair as the element you wanted, and value in that pair will be array containing that element.
Important: depending of the language you're working with, don't copy the arrays as values, but save only pointers to them (let's say in C++), or their references (i.e. Java).
Let's say that the 'randomness' of an array is equal to the number of swap one needs to do in order to have the array as completely sorted.
I want to write a program that would take in a sorted input array and as output would return an array with maximum 'randomness'.
Please help.
Depends on how you define "swap".
For example, if your array was [1,2,3,4,5] and swaps must be between adjacent numbers, then the array with maximum "randomness" would be the reverse [5,4,3,2,1]. However, if swaps can be between any two numbers, then the array with maximum "randomness" would be any array that takes (array length)-1 swaps to sort, such as [5,1,4,2,3]
There are many possible arrays that require the maximum number of swaps to sort. For example, take the numbers 1 through 5.
2,3,4,5,1
5,4,3,2,1
Both require five swaps to produce the sorted array, because all elements are out of position.
I have an array
A[4]={4,5,9,1}
I need it would give the first 3 top elements like 9,5,4
I know how to find the max element but how to find the 2nd and 3rd max?
i.e if
max=A[0]
for(i=1;i<4;i++)
{
if (A[i]>max)
{
max=A[i];
location=i+1;
}
}
actually sorting will not be suitable for my application because,
the position number is also important for me i.e. I have to know in which positions the first 3 maximum is occurring, here it is in 0th,1th and 2nd position...so I am thinking of a logic
that after getting the max value if I could put 0 at that location and could apply the same steps for that new array i.e.{4,5,0,1}
But I am bit confused how to put my logic in code
Consider using the technique employed in the Python standard library. It uses an underlying heap data structure:
def nlargest(n, iterable):
"""Find the n largest elements in a dataset.
Equivalent to: sorted(iterable, reverse=True)[:n]
"""
if n < 0:
return []
it = iter(iterable)
result = list(islice(it, n))
if not result:
return result
heapify(result)
for elem in it:
heappushpop(result, elem)
result.sort(reverse=True)
return result
The steps are:
Make an n length fixed array to hold the results.
Populate the array with the first n elements of the input.
Transform the array into a minheap.
Loop over remaining inputs, replacing the top element of the heap if new data element is larger.
If needed, sort the final n elements.
The heap approach is memory efficient (not requiring more memory than the target output) and typically has a very low number of comparisons (see this comparative analysis).
You can use the selection algorithm
Also to mention that the complexity will be O(n) ie, O(n) for selection and O(n) for iterating, so the total is also O(n)
What your essentially asking is equivalent to sorting your array in descending order. The fastest way to do this is using heapsort or quicksort depending on the size of your array.
Once your array is sorted your largest number will be at index 0, your second largest will be at index 1, ...., in general your nth largest will be at index n-1
you can follw this procedure,
1. Add the n elements to another array B[n];
2. Sort the array B[n]
3. Then for each element in A[n...m] check,
A[k]>B[0]
if so then number A[k] is among n large elements so,
search for proper position for A[k] in B[n] and replace and move the numbers on left in B[n] so that B[n] contains n large elements.
4. Repeat this for all elements in A[m].
At the end B[n] will have the n largest elements.
Given an array(unsorted) of n integers and an integer 'X'. We are also given a range (low,high).
How can we find the number of occurrences of 'X' in the range (low,high). Can we do it by Segment tree..??
As the array is unsorted you need to iterate through the whole array at least once. Therefore there is no better solution than to iterate through the whole array and just count the occurrences of 'X'. The solution requires O(n) time and O(1) memory.
Regards,
Damjan