热门标签 | 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。看到你的文章出现在极客博客主页上,帮助其他极客。
如果发现有不正确的地方,或者想分享更多关于上述话题的信息,请写评论。


推荐阅读
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • 开发笔记:加密&json&StringIO模块&BytesIO模块
    篇首语:本文由编程笔记#小编为大家整理,主要介绍了加密&json&StringIO模块&BytesIO模块相关的知识,希望对你有一定的参考价值。一、加密加密 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 本文介绍了一个Java猜拳小游戏的代码,通过使用Scanner类获取用户输入的拳的数字,并随机生成计算机的拳,然后判断胜负。该游戏可以选择剪刀、石头、布三种拳,通过比较两者的拳来决定胜负。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Python语法上的区别及注意事项
    本文介绍了Python2x和Python3x在语法上的区别,包括print语句的变化、除法运算结果的不同、raw_input函数的替代、class写法的变化等。同时还介绍了Python脚本的解释程序的指定方法,以及在不同版本的Python中如何执行脚本。对于想要学习Python的人来说,本文提供了一些注意事项和技巧。 ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
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社区 版权所有