Can anyone solve this in O(n)?
Anonymous User
683
Given an array of (n) Numbers, we have to divide the array into two subarrays such that all the elements
of subarray-1 are equal to each other and all the elements of subarray-2 are similar to each other.
And on each element, we can perform two operations-: Either increase an element by one or decrease
any element by 1.
Count the minimum operations required.
Eg-: Arr=[1 1 2 4 5 5] ---> o/p=2 as we can divide it into two subarrays
subarray-1=[1 1 2] and operation required to make all elements the same i.e. into [1 1 1] is 1, And
subarray-2=[4 5 5] and operation required to make all elements the same, ie. into [5 5 5] is 1.
Time-Complexity should be O(n).
Comments (3)