📌  相关文章
📜  从每个数组中精确删除一个元素后的一对数组,它们的和之和相等

📅  最后修改于: 2021-06-26 14:15:38             🧑  作者: Mango

给定K个不同大小的数组。任务是在从每个数组中删除一个元素后,检查是否存在两个数组具有相同的元素总和。 (可以删除任何元素,但必须删除一个元素)。如果存在这样的对,则打印数组的索引和已删除元素的索引。如果有多对,请打印其中的任何一对。如果不存在这样的对,则打印-1。

例子:

蛮力:
对于每对数组,对于每个元素,找到所选元素对的第二个数组中不包含该元素的总和,并将其与不包含每个元素的总和进行比较。
(此处maxl表示集合中数组的最大长度)。

时间复杂度\mathcal{O}(k*k*maxl*maxl)
空间复杂度\mathcal{O}(k*maxl)

高效方法:预先计算通过从每个数组中删除一个元素而获得的总和的所有可能值。存储数组索引和元素索引,将其与计算得出的和删除。当这些值以递增顺序排列时,可以很容易地看出,如果存在解,则两个和值都必须与新排列相邻。当两个相邻的和值相同时,检查它们是否属于不同的数组。如果是这样,则打印删除的元素的数组编号和索引。如果找不到这样的总和值,则不存在这样的对。

下面是上述方法的实现:

// C++program to print the pair of arrays
// whose sum are equal after removing
// exactly one element from each
#include 
using namespace std;
  
// Function to print the pair of array and index
// of element to be removed to make sum equal
void printPair(vector a[], int k)
{
  
    // stores the sum removing one element,
    // array number and index of removed element
    vector > > ans;
  
    // traverse in every array
    for (int i = 0; i < k; i++) {
  
        // length of array
        int l = a[i].size();
  
        int sum = 0;
  
        // compute total sum of array
        for (int j = 0; j < l; j++) {
            sum = sum + a[i][j];
        }
  
        // remove each element once and insert sum in
        // ans vector along with index
        for (int j = 0; j < l; j++) {
            ans.push_back({ sum - a[i][j], { i + 1, j } });
        }
    }
  
    // sort the ans vector so that
    // same sum values after removing
    // a single element comes together
    sort(ans.begin(), ans.end());
  
    bool flag = false;
  
    // iterate and check if any adjacent sum are equal
    for (int p = 1; p < ans.size(); p++) {
  
        // check if the adjacent sum belong to different array
        // if the adjacent sum is equal
        if (ans[p - 1].first == ans[p].first
            && ans[p - 1].second.first != ans[p].second.first) {
  
            // first array number
            int ax = ans[p - 1].second.first;
  
            // element's index removed from first array
            int aidx = ans[p - 1].second.second;
  
            // second  array number
            int bx = ans[p].second.first;
  
            // element's index removed from second array
            int bidx = ans[p].second.second;
  
            cout << "Array " << ax << ", index " << aidx << "\n";
            cout << "Array " << bx << ", index " << bidx << "\n";
  
            flag = true;
            break;
        }
    }
  
    // If no pairs are found
    if (!flag)
        cout << "No special pair exists\n";
}
  
// Driver Code
int main()
{
    // k sets of array
    vector a[] = {
        { 8, 1, 4, 7, 1 },
        { 10, 10 },
        { 1, 3, 4, 7, 3, 2, 2 }
    };
    int k = sizeof(a) / sizeof(a[0]);
  
    // Calling Function to print the pairs if any
    printPair(a, k);
  
    return 0;
}
输出:
Array 1, index 4
Array 3, index 5

时间复杂度\mathcal{O}(\sum_{i=1}^{i=k}l_{i})或简单地说, \mathcal{O}(k*maxl)
空间复杂度\mathcal{O}(\sum_{i=1}^{i=k}l_{i})或简单地说, \mathcal{O}(k*maxl)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。