![]() |
VOOZH | about |
Given an array arr[] consisting of N positive integers, the task is to print the minimum number of steps required to make the array such that all elements are equal by performing the following operations on the array any number of times (possibly 0).
Examples:
Input: arr[] = {1, 2, 12, 20, 18, 19}
Output: 2
Explanation: It can be solved in two steps:
In the first step, apply operation 2 by choosing L = 4 and R = 6 yields the sequence {1, 2, 12, 18, 19, 20}.
In the second step, apply operation 1 on the prefix arr{1, 2, 12, 18, 19, 20} which converts arr into {20, 20, 20, 20, 20} where all elements are equal.Input: arr[] = {2, 2, 2, 2}
Output: 0
Explanation: the elements in the array are all same.
Approach: The given problem can be solved by using the following observation:
So based on the above observation, the answer can be 0, 1, or 2. Follow the steps mentioned below.
Below is the implementation of the above approach:
2
Time Complexity: O(N)
Auxiliary Space: O(1)