I want to rank and unrank binary permutations with a distance criteria.
Example:
maxDistance := 2
Valid binary permutations consisting the elements 0011 are following:
0101 -> Rank 0
1010 -> Rank 1
0110 -> invalid because the first index of 0 is 0 but the second occurence of 0 has index 3. 3-0 > maxDistance
0011 -> invalid because the first occurence of 1 is at index 2 but the distance is 3.
distance is calculated, via equal elements index, of two adjecent equal elements doesnt differ more than abs(distance), but for the first occurence of an element the index+1 == distance.
The maxDistance applies for 0 and 1.
There can be unequal occurences of 0,1 in the binary permutation.
Rank is the index of the given permutation in an ordered list of valid permutations.
to rank = binary permutation to indexnumber(Rank), maxDistance
to unrank = Rank+occurences 0+occurences 1+maxDistance to permutation
int calcMaxDistance(BitSet set){
int max0 =0;
int max1 =0;
int lastIndex0 =0;
int lastIndex1 =0;
for(int i=0; i < set.size();i++){
if(set.get(i)){
if(lastIndex1 ==0 && max1 ==0){
max1 = i+1;
}else{
if(i - lastIndex1 > max1){
max1 = i - lastIndex1;
}
}
lastIndex1 = i;
}else{
// Same structure for max0 like with max1
}
}
return Math.max(max0,max1);
}
Has anybody an idea how to construct the algorithm?