Minimum sum of two elements from two arrays such that indexes are not same

Given two arrays a[] and b[] of same size. Task is to find minimum sum of two elements such that they belong to different arrays and are not at same index in their arrays.

Examples:

Input : a[] = {5, 4, 13, 2, 1}
        b[] = {2, 3, 4, 6, 5}
Output : 3
We take 1 from a[] and 2 from b[]
Sum is 1 + 2 = 3.


Input : a[] = {5, 4, 13, 1}
        b[] = {3, 2, 6, 1}
Output : 3
We take 1 from a[] and 2 from b[].
Note that we can't take 1 from b[]
as the elements can not be at same
index. 

A simple solution is to consider every element of a[], form its pair with all elements of b[] at indexes different from its index and compute sums. Finally return the minimum sum. Time complexity of this solution is O(n2)



An efficient solution works in O(n) time. Below are steps.

  1. Find minimum elements from a[] and b[]. Let these elements be minA and minB respectively.
  2. If indexes of minA and minB are not same, return minA + minB.
  3. Else find second minimum elements from two arrays. Let these elements be minA2 and minB2. Return min(minA + minB2, minA2 + minB)

Below is the implementation of above idea:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find minimum sum of two
// elements chosen from two arrays such that
// they are not at same index.
#include <bits/stdc++.h>
using namespace std;
  
// Function which returnss minimum sum of two
// array elements such that their indexes are
// not same
int minSum(int a[], int b[], int n)
{
    // Finding minimum element in array A and
    // also/ storing its index value.
    int minA = a[0], indexA;
    for (int i=1; i<n; i++)
    {
        if (a[i] < minA)
        {
            minA = a[i];
            indexA = i;
        }
    }
  
    // Finding minimum element in array B and
    // also storing its index value
    int minB = b[0], indexB;
    for (int i=1; i<n; i++)
    {
        if (b[i] < minB)
        {
            minB = b[i];
            indexB = i;
        }
    }
  
    // If indexes of minimum elements are
    // not same, return their sum.
    if (indexA != indexB)
        return (minA + minB);
  
    // When index of A is not same as previous
    // and value is also less than other minimum
    // Store new minimum and store its index
    int minA2 = INT_MAX, indexA2;
    for (int i=0; i<n; i++)
    {
        if (i != indexA && a[i] < minA2)
        {
            minA2 = a[i];
            indexA2 = i;
        }
    }
  
    // When index of B is not same as previous
    // and value is also less than other minimum.
    // Store new minimum and store its index
    int minB2 = INT_MAX, indexB2;
    for (int i=0; i<n; i++)
    {
        if (i != indexB && b[i] < minB2)
        {
            minB2 = b[i];
            indexB2 = i;
        }
    }
  
    // Taking sum of previous minimum of a[]
    // with new minimum of b[]
    // and also sum of previous minimum of b[]
    //  with new minimum of a[]
    // and return whichever is minimum.
    return min(minB + minA2, minA + minB2);
}
  
// Driver code
int main()
{
    int a[] = {5, 4, 3, 8, 1};
    int b[] = {2, 3, 4, 2, 1};
    int n = sizeof(a)/sizeof(a[0]);
    cout << minSum(a, b, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find minimum sum of two
// elements chosen from two arrays such that
// they are not at same index.
  
class Minimum{
  
    // Function which returns minimum sum of two
    // array elements such that their indexes are
    // not same
    public static int minSum(int a[], int b[], int n)
        {
           // Finding minimum element in array A and
           // also/ storing its index value.
           int minA = a[0], indexA = 0;
           for (int i=1; i<n; i++)
           {
               if (a[i] < minA)
               {
                   minA = a[i];
                   indexA = i;
               }
           }
   
           // Finding minimum element in array B and
           // also storing its index value
           int minB = b[0], indexB = 0;
           for (int i=1; i<n; i++)
           {
              if (b[i] < minB)
              {
                  minB = b[i];
                  indexB = i;
              }
           }
   
            // If indexes of minimum elements are
            // not same, return their sum.
            if (indexA != indexB)
            return (minA + minB);
   
            // When index of A is not same as previous
            // and value is also less than other minimum
            // Store new minimum and store its index
            int minA2 = Integer.MAX_VALUE, indexA2 = 0;
            for (int i=0; i<n; i++)
            {
               if (i != indexA && a[i] < minA2)
               {
                   minA2 = a[i];
                   indexA2 = i;
               }
            }
   
            // When index of B is not same as previous
            // and value is also less than other minimum.
            // Store new minimum and store its index
            int minB2 = Integer.MAX_VALUE, indexB2 = 0;
            for (int i=0; i<n; i++)
            {
                if (i != indexB && b[i] < minB2)
                {
                   minB2 = b[i];
                   indexB2 = i;
                }
            }
   
            // Taking sum of previous minimum of a[]
            // with new minimum of b[]
            // and also sum of previous minimum of b[]
            // with new minimum of a[]
            // and return whichever is minimum.
            return Math.min(minB + minA2, minA + minB2);
        }
      
    public static void main(String[] args)
    {
        int a[] = {5, 4, 3, 8, 1};
        int b[] = {2, 3, 4, 2, 1};
        int n = 5;
        System.out.print(minSum(a, b, n));
    }
}
  
// This code is contributed by rishabh_jain

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find minimum sum of 
# two elements chosen from two arrays
# such that they are not at same index.
import sys
  
# Function which returnss minimum sum 
# of two array elements such that their
# indexes arenot same
def minSum(a, b, n):
      
    # Finding minimum element in array A 
    # and also storing its index value.
    minA = a[0]
    indexA = 0
    for i in range(1,n):
        if a[i] < minA:
            minA = a[i]
            indexA = i
              
    # Finding minimum element in array B
    # and also storing its index value
    minB = b[0]
    indexB = 0
    for i in range(1, n):
        if b[i] < minB:
            minB = b[i]
            indexB = i
              
    # If indexes of minimum elements 
    # are not same, return their sum.
    if indexA != indexB:
        return (minA + minB)
      
    # When index of A is not same as 
    # previous and value is also less 
    # than other minimum. Store new 
    # minimum and store its index
    minA2 = sys.maxsize
    indexA2=0
    for i in range(n):
        if i != indexA and a[i] < minA2:
            minA2 = a[i]
            indexA2 = i
              
    # When index of B is not same as 
    # previous and value is also less 
    # than other minimum. Store new 
    # minimum and store its index
    minB2 = sys.maxsize
    indexB2 = 0
    for i in range(n):
        if i != indexB and b[i] < minB2:
            minB2 = b[i]
            indexB2 = i
      
    # Taking sum of previous minimum of 
    # a[] with new minimum of b[]
    # and also sum of previous minimum 
    # of b[] with new minimum of a[] 
    # and return whichever is minimum.
    return min(minB + minA2, minA + minB2)
      
# Driver code
a = [5, 4, 3, 8, 1]
b = [2, 3, 4, 2, 1]
n = len(a)
print(minSum(a, b, n))
  
# This code is contibuted by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find minimum sum of
// two elements chosen from two arrays
// such that they are not at same index.
using System;
          
public class GFG {
      
    // Function which returns minimum
    // sum of two array elements such 
    // that their indexes are not same
    static int minSum(int []a, int []b,
                                  int n)
    {
          
        // Finding minimum element in
        // array A and also/ storing its
        // index value.
        int minA = a[0], indexA = 0;
          
        for (int i = 1; i < n; i++)
        {
            if (a[i] < minA)
            {
                minA = a[i];
                indexA = i;
            }
        }
  
        // Finding minimum element in 
        // array B and also storing its
        // index value
        int minB = b[0], indexB = 0;
          
        for (int i = 1; i < n; i++)
        {
            if (b[i] < minB)
            {
                minB = b[i];
                indexB = i;
            }
        }
  
        // If indexes of minimum elements
        // are not same, return their sum.
        if (indexA != indexB)
            return (minA + minB);
              
        // When index of A is not same as
        // previous and value is also less
        // than other minimum Store new
        // minimum and store its index
        int minA2 = int.MaxValue;
          
        for (int i=0; i<n; i++)
        {
            if (i != indexA && a[i] < minA2)
            {
                minA2 = a[i];
            }
        }
  
        // When index of B is not same as
        // previous and value is also less
        // than other minimum. Store new
        // minimum and store its index
        int minB2 = int.MaxValue;
          
        for (int i=0; i<n; i++)
            if (i != indexB && b[i] < minB2)
                minB2 = b[i];
  
        // Taking sum of previous minimum
        // of a[] with new minimum of b[]
        // and also sum of previous minimum
        // of b[] with new minimum of a[]
        // and return whichever is minimum.
        return Math.Min(minB + minA2,
                            minA + minB2);
    }
      
    public static void Main()
    {
        int []a = {5, 4, 3, 8, 1};
        int []b = {2, 3, 4, 2, 1};
        int n = 5;
          
        Console.Write(minSum(a, b, n));
    }
}
  
// This code is contributed by Sam007.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find minimum 
// sum of two elements chosen 
// from two arrays such that
// they are not at same index.
  
// Function which returnss
// minimum sum of two array
// elements such that their
// indexes are not same
function minSum($a, $b, $n)
{
    // Finding minimum element
    // in array A and also 
    // storing its index value.
    $minA = $a[0];
      
    for ($i = 1; $i < $n; $i++)
    {
        if ($a[$i] < $minA)
        {
            $minA = $a[$i];
            $indexA = $i;
        }
    }
  
    // Finding minimum element 
    // in array B and also 
    // storing its index value
    $minB = $b[0];
      
    for ($i = 1; $i < $n; $i++)
    {
        if ($b[$i] < $minB)
        {
            $minB = $b[$i];
            $indexB = $i;
        }
    }
  
    // If indexes of minimum 
    // elements are not same, 
    // return their sum.
    if ($indexA != $indexB)
        return ($minA + $minB);
  
    // When index of A is not 
    // same as previous and 
    // value is also less than 
    // other minimum. Store new 
    // minimum and store its index
    $minA2 = 9999999;
    $indexA2 = 0;
    for ($i = 0; $i < $n; $i++)
    {
        if ($i != $indexA && 
            $a[$i] < $minA2)
        {
            $minA2 = $a[$i];
            $indexA2 = $i;
        }
    }
  
    // When index of B is not
    // same as previous and 
    // value is also less than 
    // other minimum. Store new
    // minimum and store its index
    $minB2 = 999999;
    $indexB2 = 0;
    for ($i = 0; $i < $n; $i++)
    {
        if ($i != $indexB && 
            $b[$i] < $minB2)
        {
            $minB2 = $b[$i];
            $indexB2 = $i;
        }
    }
  
    // Taking sum of previous 
    // minimum of a[] with
    // new minimum of b[]
    // and also sum of previous 
    // minimum of b[] with new
    // minimum of a[]
    // and return whichever 
    // is minimum.
    return min($minB + $minA2,
               $minA + $minB2);
}
  
// Driver code
$a = array(5, 4, 3, 8, 1);
$b = array(2, 3, 4, 2, 1);
$n = count($a);
echo minSum($a, $b, $n);
  
// This code is contributed 
// by Sam007
?>

chevron_right



Output:

3

Time Complexity : O(n)
Auxiliary Space : O(1)

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Improved By : Sam007



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.