How to create an array with equidistant elements?

Colleagues help to invent the algorithm.
It seems simple, but the head does not cook;

This:
``````array(
A=3,
B=6,
C=9
)``````

You want to generate an array of 18 elements whose values are equal to keys of the input array,
their number equals the value specified in the source array
The same values are far from each other

for example, the response might be:
CACBCBCBACBCBCACBC

UPDATE
There is a circle with 18 sectors
there is input
red = 3 sector
yellow = 6 sectors
green = 9 sectors
You need to evenly place the colored sector of the circle.

UPDATE2
the total sum of the values of the original array is always equal 18. The minimum value of the input element = 1
That is a limiting case of the input
A=1
B=1
C=16

Thanks in advance to all responded
July 2nd 19 at 17:55
July 2nd 19 at 17:57
Solution
that's the idea I have
for example, we need to place:
9-A
6-In
3-With
first we create an array which will be the answer
secondly, we create an array in which to store empty indices in the output array

and now actually very filling:
go those elements which are greater to those less than
1)insert 9-A
the calculated coefficient = the number of seats available in the output array/ number of inserted elements
K=18/9=2

at the moment we have such a thing

the output array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

the array is empty indexes in the output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
later in 2,4,6,8,10,12,14,16,18 elements inserted And since K=2

the output array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
A A A A A A A A A

the array is empty indexes in the output
1 2 3 4 5 6 7 8 9
1 3 5 7 9 11 13 15 17

THE SECOND STEP
to believe In
K=9/6=1,5
get the indices for the uniform distribution should round down
1,5=1
3=3
4,5=4
6=6
7,5=7
9=9
it turns out that for this index in the array of empty seats are the indices of those places where you want to insert In
get the output array of this type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
IN AND AND IN AND IN AND AND IN AND IN AND AND IN AND
going again to the same three get the output array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
IN AND AND IN AND IN AND AND IN AND IN AND AND IN AND
Yes, so thank you! - ollie.Homenick commented on July 2nd 19 at 18:00
July 2nd 19 at 17:59
Good results were one of the first versions with a simple patch on replays.
``````#include <iostream>

struct Sector
{
char name;
int total, counter, remainder;
bool allowNearby;
};

enum { N = 3 };
Sector secs[] { { 'A', 3 }, { 'B', 6 }, { 'C', 30 } };

int main()
{
int nTotal = 0;
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
nTotal += sec.total;
}
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
sec.counter = sec.total;
sec.remainder = sec.total;
sec.allowNearby = (sec.total * 2 > nTotal);
}

Sector* repeatS = nullptr;
for (int iSec = 0; iSec < nTotal; ++iSec) {
Sector* bestS = nullptr;
for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
if (sec.remainder != 0 && &sec != repeatS) {
if (!bestS) { // first any?
bestS = &sec;
} else { // better bestS?
if (sec.counter < bestS->counter
|| (sec.counter == bestS->counter && sec.total > bestS->total))
bestS = &sec;
}
}
}
bestS = repeatS;

// recalculate the counter and the rest
bestS->counter += nTotal * 2;
--bestS->remainder;

for (int i = 0; i < N; ++i) {
Sector& sec = secs[i];
sec.counter -= sec.total * 2;
}
repeatS = bestS->allowNearby ? nullptr : bestS;
std::cout << bestS->name;
}
std::cout << std::endl;
}</iostream>``````

Write on "sea cross". Since you are working in PHP, instead of pointers have to keep the "best index" and "last index".

The principle is simple. Counters believe down at different speeds; the most common sector, the faster. The priority score is: not yet exhausted → without retry → which the counter is less than → which is more common. However, if some sectors more than 50%, for him the repetition is quite a allowed.
The real test of quality is somewhat vague, and therefore challenge yourself to put really can not, respectively, and solve it really can not.

Your solution is a little misunderstood. You can ask for those "who are in the tank" a little bit harder to chew? - ollie.Homenick commented on July 2nd 19 at 18:02
If we are not dealing with the line and the circle, my solution is optimal, moreover, looks very nice. And simple, like a log.
The only problem is that with an equal fraction, we must take the winner in a specific way, first or last. But I think it will automatically.

Intuitive: look, what was the percentage of red (yellow, green) sectors. Who has it more that sector put. - ollie.Homenick commented on July 2nd 19 at 18:05
UPD: I was wrong, and an equal fraction to take the one who has more total. - Humberto_Lynch68 commented on July 2nd 19 at 18:08
I understand acting like this:
circle blank
9 green - set green
8 green - set green
7 green - set green
...
or I don't get it :-))))))))))))) - Humberto_Lynch68 commented on July 2nd 19 at 18:11
Yes, and still I was wrong. Think on. - ollie.Homenick commented on July 2nd 19 at 18:14
computed for each color step
and begin to place with the least
in our case, red

K=====TO=====TO=====
Then next, start to put, but if the place is busy then move to the step
QOL==LCD=X==QL==W==W
the rest filled green
but these will be places where green comes in a row. And I would not want. Hands can be placed more "loosely" - Humberto_Lynch68 commented on July 2nd 19 at 18:17
Yeah, and the algorithm stalled, although not immediately.
Step 0: counters 5:11:17, the share of 1.66−ε/3 : 1,83−ε/6 : 1,88−ε/9. Take green, S
Step 1: counters 5:11:15, the share of 1.66−ε/3 : 1,83−ε/6 : 1,66−ε/9. Take yellow, SG
Step 2: 5 counters:9:15, the share of 1.66−ε/3 : 1,5−ε/6 : 1,66−ε/9. Again, take green, SIS
Step 3: 5 counters:9:13, the share of 1.66−ε/3 : 1,5−ε/6 : 1,44−ε/9. Take the red, SISK
Step 4: counters 4:9:15, share 1−ε/3 : 1,5−ε/6 : 1,66−ε/9. Green, SISKS
Step 5: counters 4:9:13, share 1−ε/3 : 1,5−ε/6 : 1,44−ε/9. Yellow, SESKSI
Step 6: 3 counters:7:13, share 1−ε/3 : 1,16−ε/6 : 1,44−ε/9. Green, ZISSIS
Step 7: meters 3:7:11, share 1−ε/3 : 1,16−ε/6 : 1,22−ε/9. Green, DISKSIZES!!!!!
Think on. - ollie.Homenick commented on July 2nd 19 at 18:20
: and dologite this algorithm further? to 18 step. if there is only 1 repeat this may be what we need - Humberto_Lynch68 commented on July 2nd 19 at 18:23
Is not the same! - ollie.Homenick commented on July 2nd 19 at 18:26
Wait till night, my interested, but it is necessary to stick. Home. - Humberto_Lynch68 commented on July 2nd 19 at 18:29
: Thanks for sharing :-) I will wait for your option. while distribute another idea. - Humberto_Lynch68 commented on July 2nd 19 at 18:32
Sorry, couldn't — had to sit up to night. - ollie.Homenick commented on July 2nd 19 at 18:35
Wrote something on an old habit for "sea of crosses". - Humberto_Lynch68 commented on July 2nd 19 at 18:38
Added a little reassurance. - Humberto_Lynch68 commented on July 2nd 19 at 18:41

Find more questions by tags ArraysAlgorithms