This approach utilizes a max-heap to efficiently retrieve the largest element while adjusting it within the array to reduce deviation.
1. First, we convert all numbers to even (since an odd number can only become larger when even via multiplication). This is done by multiplying odd numbers by 2.
2. We utilize a max-heap where each iteration involves reducing the maximum number (if even) to reduce the deviation.
3. Throughout this process, track the minimum seen number to calculate and update the minimum deviation at every stage.
Time Complexity: O(n log n) due to sorting operations on each iteration.
Space Complexity: O(n) for the heap structure representation.
1#include <stdio.h>
2#include <limits.h>
3#include <stdlib.h>
4
5int compare(const void *a, const void *b) {
6 return *(int*)b - *(int*)a;
7}
8
9int minimumDeviation(int* nums, int numsSize){
10 int min_val = INT_MAX;
11 int* heap = (int*)malloc(sizeof(int) * numsSize * 2);
12 int heapSize = 0;
13
14 for (int i = 0; i < numsSize; ++i) {
15 if (nums[i] % 2 == 1) nums[i] *= 2;
16 min_val = nums[i] < min_val ? nums[i] : min_val;
17 heap[heapSize++] = nums[i];
18 }
19
20 qsort(heap, heapSize, sizeof(int), compare);
21
22 int min_deviation = INT_MAX;
23
24 while (1) {
25 int max_val = heap[0];
26 min_deviation = min_deviation < (max_val - min_val) ? min_deviation : (max_val - min_val);
27 if (max_val % 2 == 1) break;
28 max_val /= 2;
29 min_val = min_val < max_val ? min_val : max_val;
30
31 heap[0] = max_val;
32 qsort(heap, heapSize, sizeof(int), compare);
33 }
34
35 free(heap);
36 return min_deviation;
37}
The core idea is to use sorting as a form of managing the max-heap. We multiply each odd by 2 and maintain an array. Sort it to effectively manage the largest element.
The element changes are managed in situ over each comparison step, reducing the maximum each time by half if it's even and updating the reference minimum.
In this alternative approach, the strategy focuses on an incremental adjustment instead of heap reliance. It involves directly mutating and managing the adjustment in a sorted sequence approach to target deviation reduction without standard heap operations.
1. First, transform any odd number by doubling it. Track the preliminary minimum number.
2. Sort the array and iteratively adjust the maximum through division by halving until reaching an odd number.
The minimum deviation is kept through direct comparison evaluations across the deviations achieved after each max adjustment.
Time Complexity: O(n^2 log n) due to multiple sorts.
Space Complexity: O(1) or O(n) considering in-place sorts might not require extra space.
1#include <iostream>
2#include <vector>
3#include <algorithm>
4
5using namespace std;
6
7int minimumDeviation(vector<int>& nums) {
8 for (auto &num : nums)
9 if (num % 2 == 1) num *= 2;
10
11 sort(nums.begin(), nums.end());
12 int minDeviation = nums.back() - nums.front();
13
14 while (nums.back() % 2 == 0) {
15 nums.back() /= 2;
16 sort(nums.begin(), nums.end());
17 minDeviation = min(minDeviation, nums.back() - nums.front());
18 }
19
20 return minDeviation;
21}
This C++ method establishes deviation reduction by direct manipulation within a sorting framework, enhancing deviation tracking without reliance on a priority queue.
It leverages basic direct list sorting after direct maximal element alterations.