Minimum common sum from K arrays after removing some part of their suffix using C++


When working with C++ arrays we may sometimes need to calculate the minimum common sum among multiple arrays while removing a portion of their suffixes. In this article. We will explore an effective solution to this problem using C++.

Syntax

Let us begin by analyzing the syntax of our chosen method before we proceed with implementing it in our code −

int findMinimumCommonSum(vector<vector<int>>& arrays, int suffixToRemove);

Algorithm

Here is the step-by-step algorithm to solve the problem of finding the minimum common sum after removing a part of the arrays' suffixes −

  • Start by defining the function findMinimumCommonSum that takes two parameters − arrays, which is a 2D vector representing the arrays, and suffixToRemove, an integer indicating the number of elements to remove from the suffix of each array.

  • Initialize a variable minimumSum to store the minimum common sum and set it to a large value initially.

  • Iterate over each array in the arrays vector.

  • Determine the size of the current array.

  • To avoid ending up with an empty array, one should consider skipping iterations where suffixToRemove exceeds or equals the total size of the current array. Removing all characters in such a case would not produce any meaningful output.

  • Calculate the sum of the array elements from index 0 to size - suffixToRemove - 1 and store it in a variable currentSum.

  • If currentSum is less than minimumSum, update minimumSum with the value of currentSum.

  • After iterating over all arrays, minimumSum will contain the minimum common sum among the arrays after removing the specified suffix.

Approach 1: Brute Force

In this approach, we will generate all possible combinations of the suffixes to be removed and calculate the sum for each combination. The minimum sum among all combinations will be the minimum common sum.

Example

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int findMinimumCommonSum(vector<vector<int>>& arrays, int suffixToRemove) {
   int minimumSum = INT_MAX;
   int k = arrays.size();

   for (int i = 0; i < k; i++) {
      int size = arrays[i].size();

      if (suffixToRemove >= size)
         continue;

      vector<bool> suffix(size, false);
      fill(suffix.begin() + size - suffixToRemove, suffix.end(), true);

      do {
         int currentSum = 0;
         
         for (int j = 0; j < k; j++) {
            int arraySum = 0;
            for (int l = 0; l < size; l++) {
               if (!suffix[l])
                  arraySum += arrays[j][l];
            }
            currentSum += arraySum;
         }

         if (currentSum < minimumSum)
            minimumSum = currentSum;

      } while (next_permutation(suffix.begin(), suffix.end()));
   }

   return minimumSum;
}

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

   int suffixToRemove = 1;

   int minimumCommonSum = findMinimumCommonSum(arrays, suffixToRemove);

   cout << "Minimum Common Sum: " << minimumCommonSum << endl;

   return 0;
}

Output

Minimum Common Sum: 27

Explanation

In the Brute Force approach, we aim to find the minimum common sum among multiple arrays after removing a specified number of elements from their suffixes. The approach involves generating all possible combinations of the suffixes to be removed and calculating the sum for each combination. The minimum sum among all combinations will be the minimum common sum.

To implement this approach, we define a function called findMinimumCommonSum that takes two parameters − arrays, a 2D vector representing the arrays, and suffixToRemove, an integer indicating the number of elements to remove from the suffix of each array.

Within the function, we initialize a variable minimumSum to store the minimum common sum, initially set to the maximum possible value for an int. We then iterate over each array in the arrays vector. For each array, we determine its size and check if the suffixToRemove value is smaller than the size.

If the condition is satisfied, we generate all possible combinations of suffixes using a boolean vector. We fill the last suffixToRemove elements with true and the remaining elements with false. For each array, we determine its size and check if the suffixToRemove value is smaller than the size.

We proceed by calculating the total sum of the array values that correspond to the false indicators in the suffix vector, for every combination.We repeat this process for all arrays, updating the currentSum accordingly.

Finally, we compare the currentSum with the minimumSum and update it if the currentSum is smaller. After iterating through all the arrays and combinations, the minimumSum will contain the minimum common sum after removing the specified suffixes.

Approach 2: Efficient Sorting

In this approach, we will sort the arrays in non-decreasing order and calculate the sum of the first size - suffixToRemove elements for each array. The minimum sum among all arrays will be the minimum common sum.

Example

#include <iostream>
#include <vector>
#include <algorithm>
#include <climits>

using namespace std;

int findMinimumCommonSum(vector<vector<int>>& arrays, int suffixToRemove) {
   int minimumSum = INT_MAX;
   int k = arrays.size();

   for (int i = 0; i < k; i++) {
      int size = arrays[i].size();

      if (suffixToRemove >= size)
         continue;

      sort(arrays[i].begin(), arrays[i].end());

      int currentSum = 0;
      for (int j = 0; j < size - suffixToRemove; j++)
         currentSum += arrays[i][j];

      if (currentSum < minimumSum)
         minimumSum = currentSum;
   }

   return minimumSum;
}

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

   int suffixToRemove = 1;

   int minimumCommonSum = findMinimumCommonSum(arrays, suffixToRemove);

   cout << "Minimum Common Sum: " << minimumCommonSum << endl;
   
   return 0;
}

Output

Minimum Common Sum: 3

Explanation

In the Efficient Sorting approach, we aim to find the minimum common sum among multiple arrays after removing a specified number of elements from their suffixes. This approach leverages the fact that sorting the arrays can simplify the calculation of the minimum sum.

To implement this approach, we define a function called findMinimumCommonSum that takes two parameters − arrays, a 2D vector representing the arrays, and suffixToRemove, an integer indicating the number of elements to remove from the suffix of each array.

Within the function, we initialize a variable minimumSum to store the minimum common sum, initially set to the maximum possible value for an int. We then iterate over each array in the arrays vector. For each array, we determine its size and check if the suffixToRemove value is smaller than the size.

When this prerequisite has been satisfied, one of our next steps would be sorting out all individual components making up our array in an ascending order; this method chiefly helps ensure that smaller objects are located at its initial section for enhanced arrangement and readability purposes.

Next, we calculate the sum of the first size - suffixToRemove elements in the sorted array. This corresponds to removing the specified number of elements from the suffix. We update the currentSum accordingly.

Finally, we compare the currentSum with the minimumSum and update it if the currentSum is smaller. After iterating through all the arrays, the minimumSum will contain the minimum common sum after removing the specified suffixes.

This approach is efficient because it eliminates the need for generating and iterating through all possible combinations, as in the Brute Force approach. Instead, it leverages the sorting property to simplify the calculation of the minimum sum, resulting in improved performance.

Conclusion

In this article, we explored an efficient way to find the minimum common sum among K arrays in C++ after removing a part of their suffixes. We discussed two approaches − brute force and efficient sorting. The brute force approach involves generating all combinations of suffixes, while the efficient sorting approach sorts the arrays and calculates the sum of the first few elements. Depending on the size of the arrays and the number of suffix elements to remove, the efficient sorting approach is generally more efficient. By implementing these approaches in your C++ programs, you can easily find the minimum common sum from multiple arrays while handling suffix removal efficiently.

Updated on: 25-Jul-2023

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements