Minimum difference between maximum and minimum value of Array with given Operations


In this problem, we will find the minimum difference between minimum and maximum array elements after multiplying or dividing any array elements with K.

The simple approach to solving the problem is dividing each element of the array with K if divisible, multiplying each element with K, and tracking the array's minimum and maximum elements.

Problem Statement

We have given array nums[] containing the integer values and positive integer K. We can multiply any number of the nums[] array with K or divide it by K if it is divisible. The given task is to find the minimum difference between the maximum and minimum element of the array after performing the given operations on any array elements.

Sample Examples

Input

nums = {1, 6000, 11099, 8000}; k = 12000;

Output

6000

Explanation

Initially, the maximum and minimum value of the array is 11099 and 1, respectively. After multiplying 1 with 12000, the maximum value is 12000, and the minimum value is 6000, respectively. So, the minimum difference is 6000.

Input

nums = {3, 1, 4, 10, 2, 7, 5}; k = 10;

Output

6

Explanation

If we divide 10 by 10, we get 1. So, the minimum value is 1, and the maximum value of the array is 7. The minimum difference between the maximum and minimum values is 6.

Approach

In this approach, we will store all possible values after multiplying and dividing array elements with K. After that, we will sort values and traverse them. When we have K values 1 from each index, we will find the minimum and maximum values and track the minimum difference of such values.

Algorithm

  • Step 1 − Define the list to store the pair of integers. The first element of the pair will contain the original or updated array element, and the second pair will contain its index.

  • Step 2 − Traverse the array elements. In the loop, insert the {nums[p], p}, {nums[p] * k, p}, and {nums[p] / k, p} pairs into the list. Here, nums[p] is the element at index p in the array.

  • Step 3 − Use the sort() method to sort the list.

  • Step 4 − Define the numMap named map to track the visited elements. Also, define the minDiff to store the minimum difference, minValInd, and maxValInd to store the index of the minimum and maximum elements.

  • Step 5 − Now, traverse the 'que' list.

  • Step 5.1 − Take the first pair from the 'que' list. Store the first element of the pair in the temp and the second element in the p. Also, erase the first pair from the 'que' list.

  • Step 5.2 − If minValInd is -1 or numMap[minValInd] is greater than temp, initialize the minValInd with p. Furthermore, If maxValInd is -1 or numMap[maxValInd] is less than temp, initialize the maxValInd with p.

  • Step 5.3 − Next, update the numMap[p] with the temp value.

  • Step 5.4 − Now, traverse the map and find the minimum and maximum value store in the map.

  • Step 5.5 − If maxValInd is equal to p, and numMap[maxValInd] is not equal to maxVal, find the maximum value from the map and update maxValInd according to its index.

    We have 3 values for each index in the 'que' list. So, it can be possible that the previous maximum is updated, and we need to find the other element.

  • Step 5.6 − Adjust the minValInd values as we updated the maxValInd value in the previous step.

  • Step 5.7 − If the map's size is equal to the array size, take the difference between the minimum and maximum value of the map, and update the 'minDiff' value if it is greater than the resultant value.

    Here, the size of the map equal to the size of the array means the map contains elements from each index of the array.

Example

#include <bits/stdc++.h>
using namespace std;

int findMinMax(vector<int> nums, int k) {
   vector<pair<int, int>> que;
   // Insert all possible values in the queue
   for (int p = 0; p < nums.size(); p++) {
      // Original value
      que.push_back({nums[p], p});
      // Multiply by K
      que.push_back({nums[p] * k, p});
      // Divide by K if it is divisible
      if (nums[p] % k == 0) {
         que.push_back({nums[p] / k, p});
      }
   }
   // Sort the queue
   sort(que.begin(), que.end());
   int minDiff = INT_MAX;
   // To track visited elements
   map<int, int> numMap;
   // To track the Index of minimum and maximum value
   int minValInd = -1;
   int maxValInd = -1;
   // BFS algorithm
   while (!que.empty()) {
      int temp = que[0].first;
      int p = que[0].second;
      que.erase(que.begin());
      // Initialization
      if (minValInd == -1 || numMap[minValInd] > temp) {
         minValInd = p;
      }
      if (maxValInd == -1 || numMap[maxValInd] < temp) {
         maxValInd = p;
      }
      numMap[p] = temp;
      // Finding minimum and maximum map value
      int maxVal = INT_MIN;
      int minVal = INT_MAX;
      // Traverse map
      for (auto &ele : numMap) {
         maxVal = max(maxVal, ele.second);
         minVal = min(minVal, ele.first);
      }
      // adjust max and min value after adding new value in numMap.
      // Index can be same for two elements as we add elements after multiplying and dividing also
      if (maxValInd == p && numMap[maxValInd] != maxVal) {
         for (auto &ele : numMap) {
            if (numMap[maxValInd] < ele.second) {
               maxValInd = ele.first;
            }
         }
      }        
      if (minValInd == p && numMap[minValInd] != minVal) {
         for (auto &ele : numMap) {
            if (numMap[minValInd] > ele.second) {
               minValInd = ele.first;
            }
         }
      }
      // When map contains all indexes
      if (numMap.size() == nums.size()) {
         minDiff = min(minDiff, numMap[maxValInd] - numMap[minValInd]);
      }
   }
   return minDiff;
}
int main() {
   vector<int> nums = {1, 6000, 11099, 8000};
   int k = 12000;
   cout << "The difference between minimum and maximum element after updating is " << findMinMax(nums, k);
   return 0;
}

Output

The difference between minimum and maximum element after updating is - 6000
  • Time complexity − O(N*logN) to sort the array.

  • Space complexity − O(N) to store elements into the 'que' list and map.

Conclusion

Here, we used the 'que' list and sorted it. However, we can also use the priority queue to store elements and increase the code performance.

Updated on: 25-Aug-2023

111 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements