Adapting the solution linked by you to your case, you could use the following expression to create a new field that calculates for each polygon the sum of flats of all neighbouring polygons. So in your case above, you will get a sum of 25 for flat C:
) + "flat_num"
You did not provide enough information to see if this solution is sufficients. It works only with two constraints:
There is at least one "central" polygon that borders on all others (it does not work if you would have a sum of 25 for let's say A+B+C+E+F because F only borders on A, but A does not border on B).
The sum of all neighbouring polygons is 25. If however A+B+C = 25, than you will not find this solution as E also borders on C and thus the sum will be higher.
But at least it is a heuristic starting point: as in your case C, it will give you results in some cases. In others, it will give you hints where to have a closer look.
You probably anyhow need to do this in several steps, iterating over the neighbours and counting different sums to see in which cases your conditions are fulfilled. The information you provided by now is not detailed enough to guarantee an unambiguos solution, so there is no way to give a definitive answer. What if A+B+C+E = 23 flats and D, F and G all have 2 flats each? So there would be 3 valid solutions: the four flats suming up to 23 plus one of D, F or G. Which one to select?
I just can advice on some further steps you could try:
If using QGIS 3.16, you can use the new
overlay_touches function to create a field "neighbours" that lists all the neighbours of each polygon: this can be used to calculate the sum of all possible combinations:
length ( @element ) >0