给定一个数组arr[]和一个整数K 。可以对任何数组元素执行以下操作:
- 将数组元素与K相乘。
- 如果元素可被K整除,则将其除以K 。
以上两个操作可以在任何数组元素上应用任意次数,包括零。任务是找到数组的最大值和最小值之间可能的最小差异。
例子:
Input: arr[] = {1, 5000, 9999}, K = 10000
Output: 5000
Explanation:
The minimum possible difference between maximum element and minimum element is 5000. When element 1 is multiplied by K, the maximum element of the array becomes 10000 and minimum element is 5000. And, this is the least possible value.
Input: arr[] = {1, 2, 3, 4, 5, 10, 7}, K = 5
Output: 5
Explanation:
In the first step, the elements 5 and 10 can be divided with 5 making it 1 and 2 respectively.
In the second step, 1 can be multiplied by 5. This makes 2 the minimum element and 7 the maximum element in the array. Therefore, the difference is 5.
方法:这个想法是使用优先队列作为一个多集。可以按照以下步骤计算答案:
- 最初,所有可能的值(即,数组元素乘以 K,除以 K 时获得的值)与索引一起插入多重集中。
- 从多重集中连续弹出值。在每个实例中,带有索引i的弹出值X是最大值,如果至少为所有索引弹出了一个值,则当前答案将为X – min of (max of before popped out values for an index )对于除 i 之外的所有索引。
- 如果当前差异小于到目前为止计算的差异,则更新答案。
- 这一直持续到多集中剩余元素为止。
下面是上述方法的实现:
C++
// C++ implementation of the above approach
#include
using namespace std;
// Function to calculate Minimum
// difference between maximum and
// minimum value of the array
int calculateMinDiff(int arr[], int k , int n)
{
// Variable to store minimum difference
int ans = INT_MAX;
// PriorityQueue which is used as a multiset
// to store all possible values
priority_queue< pair > pq;
// Iterate through all the values and
// add it to the priority queue
for (int i = 0; i < n; i++)
{
// If the number is divisible by k
// divide it by K and add to priority queue
if (arr[i] % k == 0)
pq.push(make_pair( arr[i] / k, i ));
// Adding number as it is
pq.push(make_pair( arr[i], i ));
// Adding number after multiplying it by k
pq.push(make_pair(arr[i] * k, i ));
}
// HashMap to keep track of current values
mapmp;
while (!pq.empty())
{
pairtemp = pq.top();
pq.pop();
mp.insert(temp);
// if for every index there is at-least
// 1 value we calculate the answer
if (mp.size() == n)
{
int min_value = INT_MAX;
for(auto x:mp)
min_value=min(min_value, x.second);
ans = min(ans, temp.first - min_value);
}
}
// Returning the calculated answer
return ans;
}
// Driver code
int main()
{
// Input Array
int arr[7] = { 1, 2, 3, 4, 5, 10, 7 };
int K = 5;
// Size of the array
int N = sizeof(arr)/sizeof(int);
// Printing final output
cout << (calculateMinDiff(arr, K, N));
}
// This code is contributed by ishayadav2918
Java
// Java implementation of the above approach
import java.io.*;
import java.util.*;
class GfG {
// Function to calculate Minimum
// difference between maximum and
// minimum value of the array
private static int calculateMinDiff(int arr[], int k)
{
// Length of the array
int n = arr.length;
// Variable to store minimum difference
int ans = Integer.MAX_VALUE;
// PriorityQueue which is used as a multiset
// to store all possible values
PriorityQueue pq
= new PriorityQueue<>((int x[], int y[]) -> x[0] - y[0]);
// Iterate through all the values and
// add it to the priority queue
for (int i = 0; i < n; i++) {
// If the number is divisible by k
// divide it by K and add to priority queue
if (arr[i] % k == 0)
pq.add(new int[] { arr[i] / k, i });
// Adding number as it is
pq.add(new int[] { arr[i], i });
// Adding number after multiplying it by k
pq.add(new int[] { arr[i] * k, i });
}
// HashMap to keep track of current values
HashMap map = new HashMap<>();
while (!pq.isEmpty()) {
int temp[] = pq.poll();
map.put(temp[1], temp[0]);
// if for every index there is at-least
// 1 value we calculate the answer
if (map.size() == n) {
ans = Math.min(ans,
temp[0]
- Collections.min(map.values()));
}
}
// Returning the calculated answer
return ans;
}
// Driver code
public static void main(String args[])
{
// Input Array
int arr[] = { 1, 2, 3, 4, 5, 10, 7 };
int K = 5;
// Printing final output
System.out.println(calculateMinDiff(arr, K));
}
}
Python3
# Python3 implementation of the above approach
import sys
# Function to calculate Minimum
# difference between maximum and
# minimum value of the array
def calculateMinDiff(arr, k , n) :
# Variable to store minimum difference
ans = sys.maxsize
# PriorityQueue which is used as a multiset
# to store all possible values
pq = []
# Iterate through all the values and
# add it to the priority queue
for i in range(n) :
# If the number is divisible by k
# divide it by K and add to priority queue
if (arr[i] % k == 0) :
pq.append((arr[i] // k, i ))
# Adding number as it is
pq.append((arr[i], i))
# Adding number after multiplying it by k
pq.append((arr[i] * k, i))
pq.sort()
pq.reverse()
# HashMap to keep track of current values
mp = {}
while (len(pq) > 0) :
temp = pq[0]
pq.pop(0)
mp[temp[0]] = temp[1]
# if for every index there is at-least
# 1 value we calculate the answer
if (len(mp) == n) :
min_value = sys.maxsize
for x in mp :
min_value = min(min_value, mp[x])
ans = min(ans, temp[0] - min_value - 1)
# Returning the calculated answer
return ans
# Input Array
arr = [ 1, 2, 3, 4, 5, 10, 7 ]
K = 5
# Size of the array
N = len(arr)
# Printing final output
print(calculateMinDiff(arr, K, N))
# This code is contributed by divyesh072019.
C#
// C# implementation of the above approach
using System;
using System.Collections.Generic;
class GFG
{
// Function to calculate Minimum
// difference between maximum and
// minimum value of the array
static int calculateMinDiff(int[] arr, int k , int n)
{
// Variable to store minimum difference
int ans = Int32.MaxValue;
// PriorityQueue which is used as a multiset
// to store all possible values
List> pq = new List>();
// Iterate through all the values and
// add it to the priority queue
for (int i = 0; i < n; i++)
{
// If the number is divisible by k
// divide it by K and add to priority queue
if (arr[i] % k == 0)
pq.Add(new Tuple( arr[i] / k, i ));
// Adding number as it is
pq.Add(new Tuple( arr[i], i ));
// Adding number after multiplying it by k
pq.Add(new Tuple(arr[i] * k, i ));
}
pq.Sort();
pq.Reverse();
// HashMap to keep track of current values
Dictionary mp = new Dictionary();
while (pq.Count > 0)
{
Tuple temp = pq[0];
pq.RemoveAt(0);
mp[temp.Item1] = temp.Item2;
// if for every index there is at-least
// 1 value we calculate the answer
if (mp.Count == n)
{
int min_value = Int32.MaxValue;
foreach(KeyValuePair x in mp)
{
min_value=Math.Min(min_value, x.Value);
}
ans = Math.Min(ans, temp.Item1 - min_value - 1);
}
}
// Returning the calculated answer
return ans;
}
// Driver code
static void Main()
{
// Input Array
int[] arr = { 1, 2, 3, 4, 5, 10, 7 };
int K = 5;
// Size of the array
int N = arr.Length;
// Printing final output
Console.WriteLine(calculateMinDiff(arr, K, N));
}
}
// This code is contributed by divyeshrabadiya07.
5
时间复杂度: O(NlogN)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live