-2

Given two arrays ar1 and ar2, how can we find sum of two elements from two arrays such that indices are not same and their sum is minimum?

However, I have written the following, but I am not sure regarding storing the sum in a temporary variable and comparing the sum with the sum in the immediately next pass:

int twinArrays(int ar1_size, int* ar1, int ar2_size, int* ar2)
{
    int temp;
    for(int i = 0; i < ar1_size;i++) 
    {
        for(int j=0; j < ar2_size; j++) {
            int sum=0;
            if( i != j) 
                sum = ar1[i] + ar2[j];
            }
        }      
        return temp;
    }
}

Suggestions in C would be helpful.

Mathieu
  • 8,840
  • 7
  • 32
  • 45
  • 3
    Not sure what you're not sure of, but one thing *is* sure: since `temp` is never initialized nor assigned any determinate value, you can bank on the caller getting nothing but indeterminate garbage from that `return temp;` – WhozCraig Feb 28 '18 at 06:57
  • 1
    What's the problem with the code? Are you asking for someone to design and write the algorithm for you? Or do you have a specific question that I'm missing? – Mad Physicist Feb 28 '18 at 06:58
  • 2
    You shouldn't need `temp` for any of this. Just move `sum` outside of both loops, assign it the first qualified value from the arrays, then run the loops and only reset `sum` to a lower value if `ar1[i] + ar2[j]` is less than the current value of `sum`. In the end, return `sum`. Not very complexity efficient, but it will work. – WhozCraig Feb 28 '18 at 07:00
  • 3
    Consider that the minimum sum will be the sum of the minima, unless they have the same index. In that case, your two options become the sum of the minimum of one array and second smallest element of the other. This is an O(n) algorithm, not O(n^2). – Mad Physicist Feb 28 '18 at 07:01
  • Can you *quickly* find the minimal sum ignoring the different indices restriction? Can you find the *second smallest* sum just as quickly? The nested loop solution is too slow. – n. m. could be an AI Feb 28 '18 at 07:01

2 Answers2

0

The minimal sum of 2 elements from 2 arrays is the sum of its minimum elements.

But, what happens when any of the arrays is 0 size...? The formulation of the question is undefined in this regard.

The following worst-case time complexity is O(N) and worst-case space complexity is O(1):

#include <limits.h>

int twinArrays(int ar1_size, int* ar1, int ar2_size, int* ar2)
{
    int min1 = INT_MAX, min2 = INT_MAX;

    for(int i = 0; i < ar1_size; i++)
        min1 = ar1[i] < min1? ar[1]: min1;

    for(int i = 0; i < ar2_size; i++)
        min2 = ar2[i] < min2? ar2[1]: min2;

    if (min1 > 0 && min2 > INT_MAX - min1) {
        /* handle overflow */
        return INT_MAX;
    } else if (min1 < 0 && min2 < INT_MIN - min1) {
        /* handle underflow */
        return INT_MIN;
    }
    return min1 + min2;
}
Alejandro Blasco
  • 1,295
  • 2
  • 20
  • 24
  • `ar[1]` seems to be a typo and should be `ar1[i]`, `ar2[1]` should be `ar2[i]`. Your code ignores the requirement, that the indexes should not be equal. – mch Feb 28 '18 at 08:27
0

Your code was not so far of one solution, but:

  • temp should have been initialized to value big enough to be under the wanted result,
  • when i != j, you should have compared sum to temp to store the result if sum was under temp,
  • return was miss placed.

So your corrected code could looks like:

#include <limits.h>

int twinArrays(int ar1_size, int* ar1, int ar2_size, int* ar2)
{
    int sum_min = INT_MAX;

    /* for each element in ar1 */
    for (int i = 0; i < ar1_size; i++)
    {

        /* for each element in ar2 */
        for (int j = 0; j < ar2_size; t++)
        {
            /* go to next j if indices are the same */
            if (i != j)
            {
                /* compute sum */
                int sum = ar1[i] + ar2[j];

                /* compare to best */
                if (sum < sum_min)
                {
                    /* remember the best */
                    sum_min = sum;
                }
            }
        }        
    }

    return sum_min;   
}
Mathieu
  • 8,840
  • 7
  • 32
  • 45