15,964,238 members
1.00/5 (1 vote)
See more:
For the problem statement, please refer to the following link of stack overflow:
javascript - Balanced System Files Partition Coding Challenge - Stack Overflow[^]

I am completely unaware on how to solve the problem, however, just found a code which seems to work fine, but I don't understand the logic of that code.

This is the code in C++:
C++
```int mostBalancedPartition(vector<int> parent, vector<int> files_size) {
int total_size=accumulate(files_size.begin(),files_size.end(),0);
int sum=INT_MAX;

int i=files_size.size();

while(--i){
files_size[parent[i]]+=files_size[i];
sum = min(sum, abs(total_size - 2 * files_size[i]));
}
return sum;
}```

Can someone please explain me the logic behind this line:
C++
`sum = min(sum, abs(total_size - 2 * files_size[i]))`

What I have tried:

I tried to incorporate the code, however, even after many attempts i am unable to understand the logic, especially why we are multiplying the file size by 2 before subtracting.
Posted
Updated 30-Sep-22 2:15am
v3
Richard MacCutchan 16-Dec-21 3:54am
You should ask the person on StackOverflow who wrote the code.
Richard MacCutchan 16-Dec-21 4:43am
"why we are multiplying the file size by 2 before subtracting."
It is not multiplying the file size by 2. The variable `files_size` is a vector of integers. So unless you know what those values represent it is impossible to understand the code.
Rick York 16-Dec-21 19:58pm
If this is a coding challenge then you need to figure it out yourself.

## Solution 1

In C (and thus C++) any non-zero value is `true` - so the loop you found will continue to loop until i has been reduced to zero.

Challenges aren't about "finding a code" and submitting it: they are supposed to be "learning experiences" where you develop your own solution to a unique problem and learn something about how to analyse a problem, design a solution, and implement it in an efficient and effective manner. "Finding code" wouldn't do that, even if it was there, which it won't be as it is a "unique problem" that requires a specific solution.

If you don't understand code as simple as that fragment, then you aren't ready for that level of challenge yet! Try something simpler, that you can do on your own - you will enjoy it more, and learn more so you can move up to more complex challenges.

Siddhant Arya 16-Dec-21 2:43am
Sir, I didn't say that I have a problem in understanding of while loop or its working. My main doubt is why are we multiplying the file size by 2.

## Solution 2

OriginalGriff is right. In other languages such code wouldnt compile. A bit nasty but good for code quality.

Explanation
Better is to write
C++
```while(--i > 0 )
```
because when i starts with zero the prefix computes to -1 and than the loop runs a for a while.

So it is a BUG

## Solution 3

The first value in array is the total of all file sizes. Which includes the file size of the directory that is being subtracted to find the smallest difference, therefore it needs to be deducted twice, once so the total value is without the current directory and twice so we can find the difference in the directory sizes.