Javatpoint Logo
Javatpoint Logo

Minimize Deviation In Array

Problem Statement:

You are given an array nums of n positive integers.

You can perform two types of operations on any element of the array any number of times:

If the element is even, divide it by 2.

For example, if the array is [1,2,3,4], then you can do this operation on the last element, and the array will be [1,2,3,2].

If the element is odd, multiply it by 2.

For example, if the array is [1,2,3,4], then you can do this operation on the first element, and the array will be [2,2,3,4].

The deviation of the array is the maximum difference between any two elements in the array.

Return the minimum deviation the array can have after performing some number of operations.

Java Implementation

Java Approach Using TreeSet

Output:

Minimize Deviation In Array

Code Explanation:

  • The code defines a class MinimumDeviation with a method minimumDeviation to find the minimum deviation among elements in an array. It uses a TreeSet to store elements after doubling the odd numbers. The algorithm iteratively calculates the difference between the maximum and minimum values, updating the minimum deviation. If the maximum element is even, it divides the maximum by 2 and continues the process.
  • The iteration stops when the maximum element becomes odd. The code leverages a TreeSet to maintain a sorted order, facilitating efficient access to the minimum and maximum elements.

Time Complexity:

  • The time complexity is O(n log n), where n is the number of elements in the input array. This complexity arises from the operations involving the TreeSet.

Space Complexity:

  • The space complexity is O(n), representing the space used by the TreeSet to store the elements of the modified array, ensuring uniqueness and facilitating efficient retrieval of minimum and maximum values.

Java Approach Using Priority queue

Output:

Minimize Deviation In Array

Code Explanation:

  • The code defines a class MinimumDeviation with a method minimumDeviation to find the minimum deviation among elements in an array. It utilizes a PriorityQueue in reverse order to efficiently retrieve the maximum element. The algorithm doubles the odd numbers, adds them to the PriorityQueue, and tracks the minimum value.
  • It then iteratively calculates the deviation between the maximum and minimum values, updating the minimum deviation. If the maximum value is even, it divides the maximum by 2 and continues the process. The iteration stops when the maximum element becomes odd.

Time Complexity:

  • The time complexity is O(n log n), where n is the number of elements in the input array. This arises from the operations on the PriorityQueue.

Space Complexity:

  • The space complexity is O(n), representing the space used by the PriorityQueue to store the modified array elements.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA




news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news
news