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

• Difficulty Level : Easy
• Last Updated : 07 Oct, 2021

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.

Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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++

 // C++ program to find minimum sum of two// elements chosen from two arrays such that// they are not at same index.#include using namespace std; // Function which returns minimum sum of two// array elements such that their indexes are// not sameint minSum(int a[], int b[], int n){    // Finding minimum element in array A and    // also/ storing its index value.    int minA = a, indexA;    for (int i=1; i

Java

 // 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, indexA = 0;           for (int i=1; i

Python3

 # 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 returns minimum sum# of two array elements such that their# indexes arenot samedef minSum(a, b, n):         # Finding minimum element in array A    # and also storing its index value.    minA = a    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    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 codea = [5, 4, 3, 8, 1]b = [2, 3, 4, 2, 1]n = len(a)print(minSum(a, b, n)) # This code is contributed by "Sharad_Bhardwaj".

C#

 // 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, 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, 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



Javascript



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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.