About the logic of the code

how to analyse and deduce that maxm no. of rounds to balance will be the maxm no. of transfers reqd from onevpart of the array to the other at different partitions of the array ?I mean how to deduce the synonimity in b/w the two in similar prblms?

Hey @Senjuti256
I am not able to understand your question??
Are you asking how we came to this conclusion

??

Yes I did not understand how to arrive at the conclusion that no. of rounds reqd is equal to the maxm of all possible transfers across all partitions across the array?

Let me explain you with one example

Fnd average.

lets say a[i] is the list of jobs assigned to i th processor.

  1. from each job a[i] do a[i] - avg. if a[i] is +ve it means it has to export this many jobs to neighbors and -ve means it has to import this may jobs from neighbors .
  2. for all i do a[i] =a[i] + a[i - 1] i.e. find sum till i.
  3. find max absolute value from a[i] and print it.

explanation:

lets say a[i] after performing step 1 is: [ 2 ,-5, 7, -4]

after performing step 2 array will become [2, -3, 4, 0]

what it means is that first processor has 2 extra jobs and it can export it in 2 round.

2nd processor has 5 jobs to import. It can take 2 from 1st processor, it has still 3 more to import. and it can be done in 3 rounds.

3rd processor has 7 jobs to export. It can export 3 to previous processor in 3 round and it has remaining 4 jobs, which can be exported in 4 rounds to the next processor.

4th processor has to import 4 jobs and it can import it from previous processor in 4 rounds. Hence max rounds is the max absolute value from a[i].

Do hit the like button and mark this as resolved

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.