dimanche 7 juillet 2019

How to get maximum of sum of absolute differences of an array which only contains 1(s) and 0(s) by rearranging or by some pattern analysis?

Lets take an example I have an array a = [1,1,1,0,0] the arrangement of a which is [1,0,1,0,1] is giving the maximum sum of consecutive elements i.e, |1-0|+|0-1|+|1-0|+|0-1| = 4

The approach i used was pretty easy I rearranged the whole array by some algo. But if you look closely there is a pattern which depends on the number of elements in the array and the number of ones or zeros.

I'm already getting the solution , but if someone give me the pattern , that will be most fruitful.

Aucun commentaire:

Enregistrer un commentaire