给定的阵列ARR []由N个正整数,任务是由2任何奇数组元素相乘并除以2任意偶数阵列元件以最小化任何对的数组元素之间的最大差值。
例子:
Input: arr[] = {4, 1, 5, 20, 3}
Output: 3
Explanation:
Operation 1: Multiplying arr[1] by 2 modifies arr[] to {4, 2, 5, 20, 3}.
Operation 2: Dividing arr[3] by 2 modifies arr[] to {4, 2, 5, 10, 3}.
Operation 3: Dividing arr[3] by 2 modifies arr[] to {4, 2, 5, 5, 3}.
Therefore, the minimum of the maximum difference of any pair in the array = 5 – 2 = 3.
Input: arr[] = {1, 2, 5, 9}
Output: 7
Explanation:
Operation 1: Multiplying arr[0] by 2 modifies arr[] to { 2, 2, 5, 9 }
Operation 2: Multiplying arr[2] by 2 modifies arr[] to {2, 2, 10, 9 }
Therefore, the minimum of the maximum difference of any pair in the array = 9 – 2 = 7.
方法:按照以下步骤解决给定的问题:
- 首先,将所有数组元素插入到 Set S 中。如果数组元素是偶数,则按原样插入。否则,将其乘以 2 将其转换为偶数。
- 将 Set S中最后一个和第一个元素之间的差异存储在一个变量中,比如res 。
- 以相反的顺序遍历集合S并执行以下操作:
- 更新资源的最大资源的与所述第一和所述一组当前元素之间的差异的值。
- 从集合中移除当前元素。
- 将(当前元素)/ 2插入到集合中。
- 如果当前元素的值为奇数,则没有数组更多的元素可以使最大差异变小。因此跳出循环。
- 完成上述步骤后,打印res的值作为结果差异。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to minimize the maximum
// difference between any pair of elements
// of the array by the given operations
int minimumMaxDiff(vector& nums)
{
set s;
// Traverse the array
for (int i = 0; i < nums.size(); i++) {
// If current element is even
if (nums[i] % 2 == 0)
// Insert it into even
s.insert(nums[i]);
// Otherwise
else
// Make it even by multiplying
// by 2 and insert it into set
s.insert(nums[i] * 2);
}
// Calculate difference between first
// and the last element of the set
int res = *s.rbegin() - *s.begin();
// Iterate until difference is minimized
while (*s.rbegin() % 2 == 0) {
int x = *s.rbegin();
// Erase the current element
s.erase(x);
// Reduce current element by half
// and insert it into the Set
s.insert(x / 2);
// Update difference
res = min(res, *s.rbegin()
- *s.begin());
}
// Return the resultant difference
return res;
}
// Driver Code
int main()
{
vector arr = { 1, 2, 5, 9 };
cout << minimumMaxDiff(arr);
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to minimize the maximum
// difference between any pair of elements
// of the array by the given operations
static int minimumMaxDiff(int[] nums)
{
TreeSet s = new TreeSet();
// Traverse the array
for (int i = 0; i < nums.length; i++)
{
// If current element is even
if (nums[i] % 2 == 0)
// Insert it into even
s.add(nums[i]);
// Otherwise
else
// Make it even by multiplying
// by 2 and insert it into set
s.add(nums[i] * 2);
}
// Calculate difference between first
// and the last element of the set
int res = s.last() - s.first();
// Iterate until difference is minimized
while (s.last() % 2 == 0)
{
int x = s.last();
// Erase the current element
s.remove(x);
// Reduce current element by half
// and insert it into the Set
s.add(x / 2);
// Update difference
res = Math.min(res, s.last() - s.first());
}
// Return the resultant difference
return res;
}
// Driver code
public static void main(String[] args)
{
int[] arr = new int[] { 1, 2, 5, 9 };
System.out.print(minimumMaxDiff(arr));
}
}
// This code is contributed by jithin
Python3
# Python3 program for the above approach
# Function to minimize the maximum
# difference between any pair of elements
# of the array by the given operations
def minimumMaxDiff(nums):
s = {}
# Traverse the array
for i in range(len(nums)):
# If current element is even
if (nums[i] % 2 == 0):
# Insert it into even
s[nums[i]] = 1
# Otherwise
else:
# Make it even by multiplying
# by 2 and insert it into set
s[nums[i] * 2] = 1
# Calculate difference between first
# and the last element of the set
sr = list(s.keys())
res = sr[-1] - sr[0]
# Iterate until difference is minimized
while (list(s.keys())[-1] % 2 == 0):
r = list(s.keys())
x = r[-1]
# Erase the current element
del s[x]
# Reduce current element by half
# and insert it into the Set
s[x // 2] = 1
rr = list(s.keys())
# Update difference
res = min(res, rr[-1] - r[0])
# Return the resultant difference
return res
# Driver Code
if __name__ == '__main__':
arr = [ 1, 2, 5, 9 ]
print (minimumMaxDiff(arr))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
// Function to minimize the maximum
// difference between any pair of elements
// of the array by the given operations
static int minimumMaxDiff(int[] nums)
{
HashSet s = new HashSet();
// Traverse the array
for (int i = 0; i < nums.Length; i++) {
// If current element is even
if (nums[i] % 2 == 0)
// Insert it into even
s.Add(nums[i]);
// Otherwise
else
// Make it even by multiplying
// by 2 and insert it into set
s.Add(nums[i] * 2);
}
// Calculate difference between first
// and the last element of the set
int res = s.Last() - s.First();
// Iterate until difference is minimized
while (s.Last() % 2 == 0) {
int x = s.Last();
// Erase the current element
s.Remove(x);
// Reduce current element by half
// and insert it into the Set
s.Add(x / 2);
// Update difference
res = Math.Min(res, s.Last() - s.First());
}
// Return the resultant difference
return res;
}
// Driver code
static public void Main()
{
int[] arr = new int[] { 1, 2, 5, 9 };
Console.WriteLine(minimumMaxDiff(arr));
}
}
// This code is contributed by Dharanendra L V
Javascript
7
时间复杂度: O(N*log N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live