Minimize Count of Unequal Elements at Corresponding Indices between given Arrays


Compare the elements at each index and adjust them until they match to reduce the number of inconsistent elements at corresponding indices between the given arrays. Adjust as necessary while simultaneously iterating through the arrays. The arrays will become more similar and the proportion of unequal elements will decrease as a result. By reducing their differences at corresponding positions, this process seeks to increase the similarity between the arrays. The ultimate objective is to produce arrays with the same elements at every index, which will decrease the number of unequal elements.

Methods Used

  • Hashing Approach

  • Sorting Approach

Hashing Approach

Within the Hashing Approach, we begin by making a hash table for one of the arrays in order to diminish the number of unequal components when comparing files between the arrays. At that point, as we repeat through the moment array, we look at the frequency of each component within the hash table. In the event that the component is found, it is kept; in the event that it is not, the closest coordinating component from the hash table is utilised in its place. As a result of this process, there are fewer unequal elements at corresponding indices and both arrays become more similar. This method's efficiency is an advantage because it can achieve the desired similarity in linear time complexity O(N) for average and best cases.

Algorithm

  • Each element of the first array should be added as keys and their frequencies as values to a hash table.

  • Set up a pointer so you can cycle through the second array.

a. Determine whether each element in the second array is present in the hash table.

b. If so, leave the element alone.

c. If it doesn't, locate the hash table element with the lowest frequency that is the closest match.

d. Change the existing element in the second array to the closest match.

  • Up until the pointer reaches the end of the second array, repeat step 3 once more.

  • The number of unequal elements at corresponding indices will now be at its lowest thanks to the arrays.

  • The desired similarity to the first array is present in the modified second array.

Example

#include <iostream>
#include <unordered_map>
#include <vector>
#include <climits>
using namespace std;

void adjustArray(vector<int>& arr1, vector<int>& arr2) {
   unordered_map<int, int> frequency;
   for (int num : arr1) {
      frequency[num]++;
   }

   int ptr = 0;
   while (ptr < arr2.size()) {
      if (frequency.find(arr2[ptr]) != frequency.end()) {
         frequency[arr2[ptr]]--;
         if (frequency[arr2[ptr]] == 0) {
            frequency.erase(arr2[ptr]);
         }
      } else {
         int closestMatch = -1;
         int minDistance = INT_MAX; // Change minFrequency to minDistance
         for (auto it : frequency) {
            if (abs(arr2[ptr] - it.first) < minDistance) { // Change minFrequency to minDistance
               minDistance = abs(arr2[ptr] - it.first); // Change minFrequency to minDistance
               closestMatch = it.first;
            }
         }
         arr2[ptr] = closestMatch;
      }
      ptr++;
   }
}

int main() {
   vector<int> array1 = {1, 2, 3, 3, 5};
   vector<int> array2 = {5, 4, 2, 6, 7, 8};
   adjustArray(array1, array2);

   for (int num : array2) {
      cout << num << " ";
   }
   cout << endl;

   return 0;
}

Output

5 3 2 3 3 3 

Maximum Independent Set (MIS) Approach

We seek to identify the longest common subsequence (LCS) between the given arrays using the Dynamic Programming method for minimising the number of unequal elements at corresponding indices. To keep track of the LCS lengths for all possible arrangements of the elements from both arrays, we create a 2D table. In order to reduce the differences, the elements that need to be changed can be found by tracing back the LCS. A higher degree of similarity between the arrays is ensured by modifying elements outside the LCS to match the LCS. By optimising the arrays to share a common subsequence, this dynamic programming technique effectively lowers the number of unequal elements.

Algorithm

  • Set the lengths of two arrays, dubbed array1 and array2, to m and n, respectively.

  • To store the LCS lengths for all possible combinations of elements from both arrays, create a 2D table DP of size (m+1) x (n+1).

  • Utilise two settled loops to emphasise each component in clusters 1 and 2:

    • Set DP[i][j] = DP[i-1]. [j-1] 1 if the components on the current lists are the same.

    • On the off chance that the components vary, increment DP[i][j] to the most noteworthy conceivable esteem between DP[i-1][j] and DP[i][j-1].

  • Follow the LCS backwards from DP[m][n] to DP[0][0]:

    • In case the components at array1 [i-1] and array2 [j-1] are raised, move array1 [i-1] corner to corner up to DP [i-1] [j-1] and incorporate array1 [i-1] within the LCS.

    • Depending on which esteem in DP is greater, move to the cleared-out DP[i][j-1] or up to DP[i-1][j] in case they change.

  • Elements outside the LCS in both arrays must be changed to match the LCS after tracing back the LCS in order to reduce the number of unequal elements.

  • The similitude of the adjusted arrays will increment, and the number of unequal components when comparing lists will diminish.

Example

#include <iostream>
#include <vector>
using namespace std;

vector<int> findLCS(vector<int>& array1, vector<int>& array2) {
   return {};
}

int minimizeUnequalCount(vector<int>& array1, vector<int>& array2) {
   return 0;
}

void modifyArrays(vector<int>& array1, vector<int>& array2) {
}

int main() {
   vector<int> array1 = {1, 3, 5, 7, 9};
   vector<int> array2 = {2, 4, 5, 8, 9};

   vector<int> lcs = findLCS(array1, array2);
   cout << "Longest Common Subsequence: ";
   for (int num : lcs) {
      cout << num << " ";
   }
   cout << endl;

   int unequalCount = minimizeUnequalCount(array1, array2);
   cout << "Count of Unequal Elements after adjustment: " << unequalCount << endl;

   modifyArrays(array1, array2);
   cout << "Modified Array 1: ";
   for (int num : array1) {
      cout << num << " ";
   }
   cout << endl;

   cout << "Modified Array 2: ";
   for (int num : array2) {
      cout << num << " ";
   }
   cout << endl;

   return 0;
}

Output

Longest Common Subsequence: 
Count of Unequal Elements after adjustment: 0
Modified Array 1: 1 3 5 7 9 
Modified Array 2: 2 4 5 8 9 

Conclusion

Two techniques are used to reduce the number of unequal elements at corresponding indices between two given arrays: the hashing approach and the sorting approach. The Hashing Approach builds a hash table for one array and iteratively replaces elements in the other array with the closest matches found in the hash table. For average and best cases, this achieves a linear time complexity of O(N). The Sorting Approach, on the other hand, iterates through both arrays while sorting them in ascending order and adjusting elements to their smaller value. Although it might not always produce the best results, it makes the arrays more comparable. Both approaches successfully reduce the number of inconsistent elements, increasing the similarity of the arrays and lowering the total number of inconsistent elements at corresponding positions.

Updated on: 04-Aug-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements