📌  相关文章
📜  两个数组中两个元素的最小和,使得索引不相同

📅  最后修改于: 2022-05-13 01:57:51.598000             🧑  作者: Mango

两个数组中两个元素的最小和,使得索引不相同

给定两个大小相同的数组 a[] 和 b[]。任务是找到两个元素的最小和,使得它们属于不同的数组并且在它们的数组中的索引不同。

例子:

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[] 的每个元素,与 b[] 的所有元素在与其索引不同的索引处形成其对并计算总和。最后返回最小总和。该解决方案的时间复杂度为 O(n 2 )

一个有效的解决方案在 O(n) 时间内起作用。以下是步骤。

  1. 从 a[] 和 b[] 中找到最小元素。让这些元素分别为 minA 和 minB。
  2. 如果 minA 和 minB 的索引不相同,则返回 minA + minB。
  3. 否则从两个数组中找到第二个最小元素。让这些元素为 minA2 和 minB2。返回 min(minA + minB2, minA2 + minB)

下面是上述想法的实现:

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


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[0], 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 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 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[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


PHP


Javascript


输出:

3

时间复杂度: O(n)
辅助空间: O(1)