Skip to content
Related Articles

Related Articles

Pair of arrays with equal sum after removing exactly one element from each
  • Difficulty Level : Medium
  • Last Updated : 02 Jul, 2018

Given K arrays of different size. The task is to check if there exist any two arrays which have the same sum of elements after removing exactly one element from each of them. (Any element can be removed, but exactly one has to be removed). Print the indices of the array and the index of the removed elements if such pairs exist. If there are multiple pairs, print any one of them. If no such pairs exist, print -1.

Examples:

Input: k = 3
a1 = {8, 1, 4, 7, 1}
a2 = {10, 10}
a3 = {1, 3, 4, 7, 3, 2, 2}
Output: Array 1, index 4
Array 3, index 5
sum of Array 1{8, 1, 4, 7, 1} without index 4 is 20.
sum of Array 3{1, 3, 4, 7, 3, 2, 2} without index 5 is 20.

Input: k = 4
a1 = {2, 4, 6, 6}
a2 = {1, 2, 4, 8, 16}
a3 = {1, 3, 8}
a4 = {1, 4, 16, 64}
Output: -1

Brute Force:
For every pair of arrays, for each element, find the sum excluding that element and compare it with the sum excluding each element one by one in the second array of the chosen pair.
(Here maxl denotes the maximum length of an array in the set).



Time Complexity : \mathcal{O}(k*k*maxl*maxl)
Space Complexity : \mathcal{O}(k*maxl)

Efficient Approach: Precompute all possible values of the sum obtained by removing one element from each of the arrays. Store the array index and element index which is removed with the computed sum. When these values are arranged in increasing order, it can easily be seen that if a solution exists, then both the sum values must be adjacent to the new arrangement. When two adjacent sum values are same, check if they belong to different arrays. If they do, print the array number and index of the element removed. If no such sum value is found, then no such pairs exist.

Below is the implementation of the above approach:




// C++program to print the pair of arrays
// whose sum are equal after removing
// exactly one element from each
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the pair of array and index
// of element to be removed to make sum equal
void printPair(vector<int> a[], int k)
{
  
    // stores the sum removing one element,
    // array number and index of removed element
    vector<pair<int, pair<int, int> > > 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<int> 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;
}
Output:
Array 1, index 4
Array 3, index 5

Time Complexity : \mathcal{O}(\sum_{i=1}^{i=k}l_{i}), or simply, \mathcal{O}(k*maxl)
Space Complexity : \mathcal{O}(\sum_{i=1}^{i=k}l_{i}), or simply, \mathcal{O}(k*maxl)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA

My Personal Notes arrow_drop_up
Recommended Articles
Page :