Minimize the maximum difference of any pair by doubling odd elements and reducing even elements by half
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[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.
Approach: Follow the steps below to solve the given problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimumMaxDiff(vector< int >& nums)
{
set< int > s;
for ( int i = 0; i < nums.size(); i++) {
if (nums[i] % 2 == 0)
s.insert(nums[i]);
else
s.insert(nums[i] * 2);
}
int res = *s.rbegin() - *s.begin();
while (*s.rbegin() % 2 == 0) {
int x = *s.rbegin();
s.erase(x);
s.insert(x / 2);
res = min(res, *s.rbegin()
- *s.begin());
}
return res;
}
int main()
{
vector< int > arr = { 1, 2, 5, 9 };
cout << minimumMaxDiff(arr);
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int minimumMaxDiff( int [] nums)
{
TreeSet<Integer> s = new TreeSet<Integer>();
for ( int i = 0 ; i < nums.length; i++)
{
if (nums[i] % 2 == 0 )
s.add(nums[i]);
else
s.add(nums[i] * 2 );
}
int res = s.last() - s.first();
while (s.last() % 2 == 0 )
{
int x = s.last();
s.remove(x);
s.add(x / 2 );
res = Math.min(res, s.last() - s.first());
}
return res;
}
public static void main(String[] args)
{
int [] arr = new int [] { 1 , 2 , 5 , 9 };
System.out.print(minimumMaxDiff(arr));
}
}
|
Python3
def minimumMaxDiff(nums):
s = {}
for i in range ( len (nums)):
if (nums[i] % 2 = = 0 ):
s[nums[i]] = 1
else :
s[nums[i] * 2 ] = 1
sr = list (s.keys())
res = sr[ - 1 ] - sr[ 0 ]
while ( list (s.keys())[ - 1 ] % 2 = = 0 ):
r = list (s.keys())
x = r[ - 1 ]
del s[x]
s[x / / 2 ] = 1
rr = list (s.keys())
res = min (res, rr[ - 1 ] - r[ 0 ])
return res
if __name__ = = '__main__' :
arr = [ 1 , 2 , 5 , 9 ]
print (minimumMaxDiff(arr))
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class GFG
{
static int minimumMaxDiff( int [] nums)
{
HashSet< int > s = new HashSet< int >();
for ( int i = 0; i < nums.Length; i++) {
if (nums[i] % 2 == 0)
s.Add(nums[i]);
else
s.Add(nums[i] * 2);
}
int res = s.Last() - s.First();
while (s.Last() % 2 == 0) {
int x = s.Last();
s.Remove(x);
s.Add(x / 2);
res = Math.Min(res, s.Last() - s.First());
}
return res;
}
static public void Main()
{
int [] arr = new int [] { 1, 2, 5, 9 };
Console.WriteLine(minimumMaxDiff(arr));
}
}
|
Javascript
<script>
function minimumMaxDiff( nums)
{
var s = new Set();
for ( var i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0)
s.add(nums[i]);
else
s.add(nums[i] * 2);
}
var tmp = [...s].sort((a,b)=>a-b)
var res = tmp[tmp.length-1] - tmp[0];
while (tmp[tmp.length-1] % 2 == 0) {
var x = tmp[tmp.length-1];
s. delete (x);
s.add(parseInt(x / 2));
tmp = [...s].sort((a,b)=>a-b)
res = Math.min(res,tmp[tmp.length-1]
- tmp[0]);
}
return res;
}
var arr = [1, 2, 5, 9];
document.write( minimumMaxDiff(arr));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...