Array pattern issue to maintain uniformity - arrays

There is an existing array of size 64 that has values 6 values distributed as 0 1 2 3 4 5 0 1 2 3 4 5 0 1 2 3 4 5 ...
Please see the image for complete data.
The number of occurrence of 0 in the array is 11 times (at every 6th index), 1 is 11 times ... where as 4 and 5 occurs 10 times each.
There is a necessity to reduce the occurrence of any of these numbers [0 to 5] to a lesser number that could be any number from 0 to 10.
For example, it could be to reduce occurrence of 0 to 6 and 1 to 9.
I am looking for a solid idea to do this. Certainly all the numbers are to be evenly distributed and not something like 0 0 0 0 0 2 2 2 2 2 2 ...
I tried to find the index/position where the reduced value has to filled (64/occurrence of 0 or 2). But at times the index collide with each other and thus is not robust one.
From the example I quoted above, number of occurrence of 0 must be changed to 6 and occurrence of 1 to 9, the result after my algorithm is below -
New location to fill 0 = (Array size)/(new occurrence of 0) = 64/6 = ~10th index
New location to fill 1 = (Array size)/(new occurrence of 1) = 64/9 = ~7 index
For filling 6 0's and 9 1's, first the array is reset after which each of the values are filled to maintain balanced distribution.
After filling 6 0's, the array would be come like this:
Then, after filling 9 1's, the array would be come like this:
The index at 55 already has value 0 and apparently 8th 1 also index to 55 that creates a collision. So I believe, this algorithm to balance the distribution does not work.
How do I populate 6 's, 9 1's and rest of the numbers {2, 3, 4, 5} in the array in a balanced way?

Related

Merge Process In-Place:

Merge Procedure in MergeSort cannot run in-place.
This is my explanation:
Does it add up?
A = 5,1,9,2,10,0
”q”: pointer to the middle of array at index 2.
We are merging element at q and those to its left with the elements to the right of q.
A= 1,5,10,0,2,12
We point to the beginning of the left side with ”i” and to that of the right with ”j”.
We point to the current position in the array with ”k”.
The algorithm starts with i=0, j=3, k=0.
If we merge in place:for k=0:i= 0, j= 3,0<1 -> A[k] =A[j] andj+ +
resulting array:A={0,5,10,0,2,12}
As we can see, we lost the value 1 already.
We will continue to lose values, for example in the next iteration:
for k=1:i= 0, j= 4,0<2 =⇒A[k] =A[i] andi+ +
resulting array:A={0,0,10,0,2,12}
It can be done using rotates of sub-arrays. There are in place merge sorts with O(n log(n)) time complexity, but these use a portion of the array as working storage. If stability is needed, then some small subset of unique values (like 2 sqrt(n)) are used to provide the space, since reordering unique values before sorting won't break stability. Getting back to to simple rotate algorithm:
1 5 10 0 2 12
0 1 5 10 2 12 0 < 1, rotate 0 into place, adjust working indexes
0 1 5 10 2 12 1 < 2, continue
0 1 2 5 10 12 2 < 5, rotate 2 into place, adjust working indexes
0 1 2 5 10 12 5 < 12, continue
0 1 2 5 10 12 10 < 12, continue, end of left run, done

Find the last smaller or equal number for every element in the array?

I have been given an array. I need to find the last (right-most) smaller or equal number for every element in the array.
For example:
2 5 1 6 4 7
2 has last smaller or equal number 1,
5 has last smaller or equal number 4 and not 1, etc.
Another example:
5 100 8 7 6 5 4 3 2 1
Here, every element has last smaller or equal number 1. I know the naive approach, i.e. O(n²), but need a better approach.
You could go from right to left and build a min array of the minimum number until now.
For your example 2 5 1 6 4 7, it would be:
Start at rightmost position:
7
4 7 (4 < 7)
4 4 7 (6 > 4)
...
So the min array for your example would be: 1 1 1 4 4 7
Now for each query, you start at the same position in the min array and go right until finding a number which is greater:
For 2:
2 5 1 6 4 7
1 1 1 4 4 7
^
------^
First element greater than 2 is 4, so return number just before = 1
For 5:
2 5 1 6 4 7
1 1 1 4 4 7
^
----------^
First element greater than 5 is 7, so return number just before = 4
To find efficiently the first element greater for each element in the input array you can use upper_bound algorith (example in C++ http://www.cplusplus.com/reference/algorithm/upper_bound/) to find the first element which is greater
Upper_bound takes log(N) time, so overall time to process every element in input array is O(NlogN)
Memory complexity is linear for the min array

correctness of fast small order statistic algorithm for odd-length array

Problem 9-3 of the textbook Intro to Algorithms (CLRS) describes a fast O(n) algorithm for finding the k-th order statistic (k-th element in the array when sorted) of a length-n array, for the particular case that k is much smaller than n. I am not certain about the correctness of this algorithm when n is odd, and want to see a way to prove that it is correct.
The basic idea is that we first split the array into two halves, the first with floor(n/2) elements, and the second with ceil(n/2) elements. Then, we "partner" each element in the first half with the corresponding element in the second half. When n is odd this leaves a remaining unpartnered element.
For each pair of partners, we make sure that the left partner is >= the right partner, swapping the two if not. Then, recursively find the k-th order statistic of the second half, mirroring any swaps made in the second half with corresponding swaps in the first half. After this, the k-th order statistic of the entire array must be either in the first k elements in the first half, or the first k elements in the second half.
My confusion comes from the case when the array length n is odd, and there is a lone element in the second half that has no partner. Since the recursion is performed on the second half, consisting of the last ceil(n/2) elements of the array, including the lone partnerless last element, and we are supposed to mirror all swaps made in second half with swaps made within the corresponding partners in the first half, it is unclear what to do when one of the swaps involves the final element, since it has no partner.
The textbook doesn't seem to take particular care on this issue, so I'm assuming that when a swap involves the final element, then just don't make any mirror moves of the partner in the first half at all. As a result, the final element simply "steals" the partner of whoever it got swapped with. However, in this case, is there an easy way to see if the algorithm is still correct? What if when the last element steals someone else's partner, the partner is actually the k-th order statistic, and gets swapped later on to an inaccessible location? The mechanics of the recursion and partitioning involving in order-statistic selection are sufficiently opaque to me such that I cannot confidently rule out that scenario.
I don't think your description of the algorithm is entirely accurate (but then the explanation you linked to is far from clear). As I understand it, the reason why the algorithm is correct for an odd-length array is as follows:
Let's first look at a few examples of even-length arrays, with n=10 and k=3 (i.e. we're looking for the third-smallest element, which is 2):
a. 5 2 7 6 1 9 3 8 4 0
b. 5 1 7 6 2 9 3 8 4 0
c. 5 0 7 6 2 9 3 8 4 1
d. 5 0 7 6 2 9 3 8 1 4
If we split the arrays into two parts, we get:
a. 5 2 7 6 1 9 3 8 4 0
b. 5 1 7 6 2 9 3 8 4 0
c. 5 0 7 6 2 9 3 8 4 1
d. 5 0 7 6 2 9 3 8 1 4
and these couples:
a. (5,9) (2,3) (7,8) (6,4) (1,0) <- 0 coupled with 1
b. (5,9) (1,3) (7,8) (6,4) (2,0) <- 0 coupled with 2
c. (5,9) (0,3) (7,8) (6,4) (2,1) <- 1 coupled with 2
d. (5,9) (0,3) (7,8) (6,1) (2,4) <- 0, 1 and 2 not coupled with each other
After comparing and swapping the couples so that their smallest element is in the first group, we get:
a. 5 2 7 4 0 9 3 8 6 1
b. 5 1 7 4 0 9 3 8 6 2
c. 5 0 7 4 1 9 3 8 6 2
d. 5 0 7 1 2 9 3 8 6 4
You'll see that the smallest element 0 will always be in the first group. The second-smallest element 1 will be either in the first group, or in the second group if it was coupled with the smallest element 0. The third-smallest element 2 will be either in the first group, or in the second group if it was coupled with either the smallest element 0 or the second-smallest element 1.
So the smallest element is in the first group, and the second- and third-smallest elements can be in either group. That means that the third-smallest element is either one of the 3 smallest elements in the first group, or one of the 2 (!) smallest elements in the second group.
a. 5 2 7 4 0 9 3 8 6 1 -> 0 2 4 + 1 3
b. 5 1 7 4 0 9 3 8 6 2 -> 0 1 4 + 2 3
c. 5 0 7 4 1 9 3 8 6 2 -> 0 1 4 + 2 3
d. 5 0 7 1 2 9 3 8 6 4 -> 0 1 2 + 3 4
So if we say that the k-th smallest element of the whole array is now one of the k-th smallest elements in either of the groups, there is an available spot in the the second group, and that's why, in an odd-length array, we'd add the uncoupled element to the second group. Whether or not the uncoupled element is the element we're looking for, it will certainly be one of the k-th smallest elements in either of the groups.
It is in fact more correct to say that the k-th smallest element is either one of the k smallest elements in the first group, or one of the k/2+1 smallest elements in the second group. I'm actually not sure that the algorithm is optimal, or even correct. There's a lot of repeated comparing and swapping going on, and the idea of keeping track of the couples and swapping elements in one group when their corresponding elements in the other group are swapped doesn't seem to make sense.

HeIp understanding Fibonacci Search

On the internet I only find code for the algorithm but I need understand in form of text first because I have trouble understand things from code only. And other description of the algorithm are very complicated for me (on Wikipedia and other sites).
Here is what I understand for far:
Let say we want search in array the element 10:
Index i 0 1 2 3 4
2 3 4 10 40
Some fibonacci number here:
Index j 0 1 2 3 4 5 6 7 8 9
0 1 1 2 3 5 8 13 21 34
First thing we do is find fibonacci number that is greater-equal to array length. Array length is 4 so we need take fibonacci number 5 that is in index position j=5.
But where we divide the array now and how continue? I really don't understand it.. Please help understand for exam...
The algorithm goes in the following way:
The length of the array is 5, so the fibonacci number which is greater than or equal to 5 is 5. The two numbers which are preceding in the Fibonacci sequence are 2 [n-2] and 3 [n-1] - (2, 3, 5).
So, arr[n-2] i.e. arr[2] is compared with the number to be searched which is 10.
If the element is smaller than the number, then the sequence is shifted 1 time to the left. Also, the previous index is saved for next iteration to give an offset for the index. In this case, since 4 is smaller, n-2 becomes 1 (1, 2, 3). arr[1 + 2(prev)] = arr[3] = 10. So, the index of the number is 3.
If the element is larger, the sequence is shifted 2 times to the left.
Always the min(n-2+offset,n)th element is compared with number to get the matching result.

Given an input array, output the minimum number of swaps for sorting the array [duplicate]

This question already has answers here:
Minimum number of swaps needed to change Array 1 to Array 2?
(8 answers)
Closed 7 years ago.
Given an input array I want to calculate the minimum number of swaps to sort the array.
I thought that its equal to inversion count but its not as depicted below:
array [6,4,3]
output: 1, just swap 6 with 3. But the inversions are actually 3.
So given an input array is there an efficient algorithm to determine the minimum number of swaps. I know that selection sort has minimum number of swaps but I also know that it is not efficient.
You can use this method:
Say this is your input array: [4 6 1 8 2 7]
Then, in sorted array, positions of elements will be, respectively: 3 4 1 6 2 5
Create a visit array: [0 0 0 0 0 0]
Start with 1'st index in position array, mark it as visited, jump to the position it represents, until the value of visit in visit[] is set.
Then, once a cycle is complete, chose the next element whose visit value is unset.
For a cycle of k elements, add k-1 to the swap counter.
Step by step in this example will be:
visiting index in position[] visit[]
position[1] = 3 [1 0 0 0 0 0]
position[3] = 1 [1 0 1 0 0 0]
Now, a cycle is formed. Since this cycle has 2 elements, add 1 to swap counter. Now we start with index 2.
position[2] = 4 [1 1 1 0 0 0]
position[4] = 6 [1 1 1 1 0 0]
position[6] = 5 [1 1 1 1 0 1]
position[5] = 2 [1 1 1 1 1 1]
Since visit[2] is set, a cycle is formed. This cycle has 4 elements, so, add 3 to swap counter.
See which next index in visit is still unset. (None). Stop here.
So, your answer is 1 + 3 = 4

Resources