<p>You are given an array <code>nums</code> of <code>n</code> positive integers.</p>

<p>You can perform two types of operations on any element of the array any number of times:</p>

<ul>
	<li>If the element is <strong>even</strong>, <strong>divide</strong> it by <code>2</code>.

	<ul>
		<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the last element, and the array will be <code>[1,2,3,<u>2</u>].</code></li>
	</ul>
	</li>
	<li>If the element is <strong>odd</strong>, <strong>multiply</strong> it by <code>2</code>.
	<ul>
		<li>For example, if the array is <code>[1,2,3,4]</code>, then you can do this operation on the first element, and the array will be <code>[<u>2</u>,2,3,4].</code></li>
	</ul>
	</li>
</ul>

<p>The <strong>deviation</strong> of the array is the <strong>maximum difference</strong> between any two elements in the array.</p>

<p>Return <em>the <strong>minimum deviation</strong> the array can have after performing some number of operations.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 1
<strong>Explanation:</strong> You can transform the array to [1,2,3,<u>2</u>], then to [<u>2</u>,2,3,2], then the deviation will be 3 - 2 = 1.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,1,5,20,3]
<strong>Output:</strong> 3
<strong>Explanation:</strong> You can transform the array after two operations to [4,<u>2</u>,5,<u>5</u>,3], then the deviation will be 5 - 2 = 3.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [2,10,8]
<strong>Output:</strong> 3
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == nums.length</code></li>
	<li><code>2 &lt;= n &lt;= 5 * 10<sup><span style="font-size: 10.8333px;">4</span></sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    //   int recursion(multiset<int>m,int dev){
    //         auto itff=m.begin();
    //         auto itee=--m.end();
    //         int devi=*itee-*itff;
    //         int ite=*itee;
    //         int itf=*itff;
    //         if((itf%2==0 && ite%2==1) || (ite==2*itf && itf%2==1)){
    //             return min(dev,devi);
    //         }
    //         if (devi==0){
    //             return 0;
    //         }
    //             int th=INT_MAX;
    //           if (itf%2==1 && ite%2==0){
    //             m.erase(itff);
    //             m.erase(itee);
    //             m.insert(itf*2);
    //              m.insert(ite/2);
              
    //          th= recursion(m,devi);
    //         }
    //         return min(min(devi,th),min(sec,fr));




    //     }
    // int minimumDeviation(vector<int>& nums) {
    //     multiset<int>m;
    //     for(int i=0;i<nums.size();i++){
    //         m.insert(nums[i]);
    //     }
    //      auto itf=m.begin();
    //         auto ite=--m.end();
    //     int dev= *ite-*itf;
  
    //     int ans=recursion(m,dev);
    //     return ans;

        
    // }
//     int recursion(std::multiset<int> m, int dev) {
//     auto itff = m.begin();
//     auto itee = --m.end();
//     int devi = *itee - *itff;
//     int ite = *itee;
//     int itf = *itff;

//     if ((itf % 2 == 0 && ite % 2 == 1) || (ite == 2 * itf && itf % 2 == 1)) {
//         return std::min(dev, devi);
//     }

//     if (devi == 0) {
//         return 0;
//     }

//     int fr = INT_MAX;
//     if (itf % 2 == 1 && ite % 2 == 1) {
//         itf = 2 * itf;
//         m.insert(itf);
//         ite = 2 * ite;
//         m.insert(ite);
//         m.erase(m.find(*itff));
//         m.erase(m.find(*itee));
//         fr = recursion(m, devi);
//     }

//     int sec = INT_MAX;
//     if (itf % 2 == 0 && ite % 2 == 0) {
//         m.insert(itf / 2);
//         m.insert(ite / 2);
//         m.erase(m.find(*itff));
//         m.erase(m.find(*itee));
//         sec = recursion(m, devi);
//     }

//     int th = INT_MAX;
//     if (itf % 2 == 1 && ite % 2 == 0) {
//         m.insert(itf * 2);
//         m.insert(ite / 2);
//         m.erase(m.find(*itff));
//         m.erase(m.find(*itee));
//         th = recursion(m, devi);
//     }

//     return std::min({devi, th, sec, fr});
// }

// int minimumDeviation(std::vector<int>& nums) {
//     std::multiset<int> m;
//     for (int i = 0; i < nums.size(); i++) {
//         m.insert(nums[i]);
//     }
//     auto itf = m.begin();
//     auto ite = --m.end();
//     int dev = *ite - *itf;

//     int ans = recursion(m, dev);
//     return ans;
// }
// int recursion(std::multiset<int> m, int dev) {
//     auto itff = m.begin();
//     auto itee = --m.end();
//     int devi = *itee - *itff;
//     int ite = *itee;
//     int itf = *itff;

//     if ((itf % 2 == 0 && ite % 2 == 1) || (ite == 2 * itf && itf % 2 == 1)) {
//         return std::min(dev, devi);
//     }
//     if (devi == 0) {
//         return 0;
//     }

//     int fr = INT_MAX;
//     if (itf % 2 == 1 && ite % 2 == 1) {
//         m.erase(itff);
//         m.erase(itee);
        
//         itf = 2 * itf;
//         ite = 2 * ite;

//         m.insert(itf);
//         m.insert(ite);
        
//         fr = recursion(m, devi);
//     }

//     // Recompute iterators after each modification
//     itff = m.begin();
//     itee = --m.end();

//     int sec = INT_MAX;
//     if (*itff % 2 == 0 && *itee % 2 == 0) {
//         m.erase(itff);
//         m.erase(itee);

//         m.insert(*itff / 2);
//         m.insert(*itee / 2);
        
//         sec = recursion(m, devi);
//     }

//     // Recompute iterators after each modification
//     itff = m.begin();
//     itee = --m.end();

//     int th = INT_MAX;
//     if (*itff % 2 == 1 && *itee % 2 == 0) {
//         m.erase(itff);
//         m.erase(itee);
//           itf= 2 * itf;
//           ite=ite/2;

//         m.insert(itf);
//         m.insert(ite);

//         th = recursion(m, devi);
//     }

//     return std::min({devi, th, sec, fr});
// }

// int minimumDeviation(std::vector<int>& nums) {
//     std::multiset<int> m;
//     for (int i = 0; i < nums.size(); i++) {
//         m.insert(nums[i]);
//     }
//     auto itf = m.begin();
//     auto ite = --m.end();
//     int dev = *ite - *itf;

//     int ans = recursion(m, dev);
//     return ans;
// }
    int minimumDeviation(std::vector<int>& nums) {
        std::priority_queue<int> maxHeap;
        int minElement = INT_MAX;

        // Convert all numbers to even and push them into the max-heap.
        for (int num : nums) {
            if (num % 2 == 1) {
                num *= 2;  // Convert odd numbers to even
            }
            maxHeap.push(num);
            minElement = std::min(minElement, num);  // Track the minimum element
        }

        int minDeviation = INT_MAX;

        // Keep reducing the largest element in the heap.
        while (!maxHeap.empty()) {
            int maxElement = maxHeap.top();
            maxHeap.pop();

            // Calculate the current deviation
            minDeviation = std::min(minDeviation, maxElement - minElement);

            // If the maximum element is odd, break the loop
            if (maxElement % 2 == 1) {
                break;
            }

            // Reduce the maximum element by dividing it by 2
            maxElement /= 2;
            minElement = std::min(minElement, maxElement);  // Update the minimum element
            maxHeap.push(maxElement);  // Push the reduced element back into the heap
        }

        return minDeviation;
    }


};
60300