热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

两个数组中两个元素的最小和,使得索引不相同原文:https://

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

原文:https://www . geesforgeks . org/minimum-sum-two-elements-two-array-indexes-not/

给定两个大小相同的数组 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 简单的解决方案是考虑 a[]的每个元素,在不同于其索引的索引处与 b[]的所有元素形成对,并计算和。最后返回最小和。该解决方案的时间复杂度为 0(n2

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


  1. 从 a[]和 b[]中查找最小元素。让这些元素分别是 minA 和 minB。

  2. 如果 minA 和 minB 的索引不一样,返回 minA + minB。

  3. 否则从两个数组中找出第二个最小元素。让这些元素是 minA2 和 minB2。返回最小值(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    {
        if (a[i]         {
            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    {
        if (b[i]         {
            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    {
        if (i != indexA && a[i]         {
            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    {
        if (i != indexB && b[i]         {
            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 <    return 0;
}

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           {
               if (a[i]                {
                   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           {
              if (b[i]               {
                  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            {
               if (i != indexA && a[i]                {
                   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            {
                if (i != indexB && b[i]                 {
                   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

Python 3

# 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 = 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 = 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 = 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 = 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         {
            if (a[i]             {
                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         {
            if (b[i]             {
                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        {
            if (i != indexA && a[i]             {
                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            if (i != indexB && b[i]                 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.

服务器端编程语言(Professional Hypertext Preprocessor 的缩写)

// PHP program to find minimum
// sum of two elements chosen
// from two arrays such that
// they are not at same index.
// Function which returns
// 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
?>

java 描述语言


输出:

3

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

本文由 萨赫勒拉吉普特 供稿。如果你喜欢 GeeksforGeeks 并想投稿,你也可以使用write.geeksforgeeks.org写一篇文章或者把你的文章邮寄到 review-team@geeksforgeeks.org。看到你的文章出现在极客博客主页上,帮助其他极客。
如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。


推荐阅读
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 向QTextEdit拖放文件的方法及实现步骤
    本文介绍了在使用QTextEdit时如何实现拖放文件的功能,包括相关的方法和实现步骤。通过重写dragEnterEvent和dropEvent函数,并结合QMimeData和QUrl等类,可以轻松实现向QTextEdit拖放文件的功能。详细的代码实现和说明可以参考本文提供的示例代码。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 本文为Codeforces 1294A题目的解析,主要讨论了Collecting Coins整除+不整除问题。文章详细介绍了题目的背景和要求,并给出了解题思路和代码实现。同时提供了在线测评地址和相关参考链接。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
会丶有那么一天
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有