Click here to Skip to main content
15,393,377 members
Please Sign up or sign in to vote.
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]))

Thanks in advance.

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 15-Dec-21 21:24pm
v3
Comments
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.

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.
   
Comments
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.
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
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900