📌  相关文章
📜  通过将奇数元素加倍并将偶数元素减半来最小化任何对的最大差异

📅  最后修改于: 2021-10-27 07:03:46             🧑  作者: Mango

给定的阵列ARR []N个正整数,任务是由2任何奇数组元素相乘并除以2任意偶数阵列元件以最小化任何对的数组元素之间的最大差值。

例子:

方法:按照以下步骤解决给定的问题:

  • 首先,将所有数组元素插入到 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)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程