Related Articles

# Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half

• Difficulty Level : Easy
• Last Updated : 08 Jun, 2021

Given an array arr[] consisting of N positive integers, the task is to minimize the maximum difference between any pair of array elements by multiplying any odd array element by 2 and dividing any even array element by 2.

Examples:

Input: arr[] = {4, 1, 5, 20, 3}
Output: 3
Explanation:
Operation 1: Multiplying arr by 2 modifies arr[] to {4, 2, 5, 20, 3}.
Operation 2: Dividing arr by 2 modifies arr[] to {4, 2, 5, 10, 3}.
Operation 3: Dividing arr 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 by 2 modifies arr[] to { 2, 2, 5, 9 }
Operation 2: Multiplying arr 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.

Approach: Follow the steps below to solve the given problem:

Below is the implementation of the above approach:

## 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<``int``>& nums)``{``    ``set<``int``> 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<``int``> 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<``int``> s = ``new` `HashSet<``int``>();` `    ``// 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

 ``
Output:
`7`

Time Complexity: O(N*log N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up