简体   繁体   English

在 O(n * log n) 时间内找到 Array 中加起来等于目标数的两个元素

[英]Find Two elements in the Array that add up to the Target number in O(n * log n) Time

I have a problem with O(n * log(n)) searching algorithm.我对O(n * log(n))搜索算法有疑问。

I have to find two numbers from an array that add up to the given number.我必须从一个数组中找到两个加起来等于给定数字的数字。

I know how O(n * log(n)) works, but I'm not sure if those two correct numbers will meet in search if I do it like this:我知道O(n * log(n))是如何工作的,但是如果我这样做,我不确定这两个正确的数字是否会在搜索中相遇:

for (int i = 0; i < n; i++)
    for (int j = 1; j <= n; j = j*2)

Is there a way to keep O(n * log(n)) complexity so that every two numbers meet in the search?有没有办法保持O(n * log(n))复杂性,以便每两个数字在搜索中相遇?

  1. sort array (O(nlogn))排序数组 (O(nlogn))

  2. for each element:对于每个元素:

    2.1 binary search to find other element that adds up to the given number (or to figure out there is none) (O(logn)) 2.1 二进制搜索以找到加起来等于给定数字的其他元素(或找出没有)(O(logn))

Step 2 has complexity O(nlogn), and so the whole algorithm has O(nlogn)步骤 2 的复杂度为 O(nlogn),因此整个算法的复杂度为 O(nlogn)

I have to find two numbers from an array that add up to the given number .我必须从一个数组中找到两个加起来等于定数字的数字

It can be done in O(n) time and utilizing additional O(n) space.它可以在O(n)时间内完成并利用额外的O(n)空间。

For that, we can index all array elements in a Map .为此,我们可以索引Map中的所有数组元素 The key of the map would be a difference between the target sum and a particular array element and value - an element itself.映射的关键目标总和与特定数组元素和之间的差异 - 元素本身。 Creation of the map requires a single iteration over the source array, ie it cost O(n) time.地图的创建需要对源数组进行一次迭代,即花费O(n)时间。

In the case when the target sum is even and there's only a single array element is exactly equal to the target sum / 2 result would be incorrect.如果目标总和是偶数并且只有一个数组元素完全等于target sum / 2 ,则结果将不正确。 To treat this case, we can count the number of elements that are equal to the half of the sum.为了处理这种情况,我们可以计算等于总和一半的元素的数量。 It can be done simultaneously with the process of generating the map to avoid performing an additional iteration (although it contradicts clean coding practices).它可以与生成地图的过程同时完成,以避免执行额外的迭代(尽管它与干净的编码实践相矛盾)。

Then we need to examine whether there's an element in the array that that matches one of the keys contained in the map .然后我们需要检查数组中是否存在与map中包含的之一匹配的元素。 If such an element has been found, then we have a pair of numbers that produces the target sum.如果找到了这样的元素,那么我们就有一对产生目标总和的数字。

So we need to perform only 2 iterations over the source array, and that result to O(n) overall time complexity.所以我们只需要对源数组执行2次迭代,结果就是O(n)的整体时间复杂度。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 高效算法在O(log(N))时间内找到排序数组中在一定范围内的整数数量? - Efficient algo to find number of integers in a sorted array that are within a certain range in O(log(N)) time? 如何在O(log(N))时间内找到排序数组中在一定范围内的整数数? - How to find number of integers in a sorted array that are within a certain range in O(log(N)) time? 在 O(n log n) 时间内生成具有 n 个长度和 k 个反转的数组的算法? - Algorithm to generate an array with n length and k number of inversions in O(n log n) time? 解决 LeetCode 问题以在 O(log n) 中找到数组中目标的第一个和最后一个索引 - Solving a LeetCode problem to find first and last index of target in array in O(log n) 在时间复杂度 O(n) 和空间复杂度 O(1) 中查找数组中重复元素的最有效算法是什么? - What is the most efficient algorithm to find repeated elements in an array in time complexity O(n) and space complexity O(1)? 关于时间复杂度 O(1), O(n), O(log n), O(n log n) 的问题 - Questions about Time complexity O(1), O(n), O(log n), O(n log n) O(n log n)时间复杂度算法? - O(n log n) Time Complexity Algorithm? 如何在O(n)中的排序数组中找到两个总和为给定数字的数字? - How to find two number whose sum is given number in sorted array in O(n)? 以O(log n)复杂度计算排序数组中值的出现次数 - Count the number of appearances of a value in a sorted array in O(log n) complexity 如何计算大小为625的数组的O(log n)处理时间? - How to calculate O(log n) processing time on an array of size 625?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM