the sum of two fixed value

description

Input an array and an integer, fina a pair of number in the array so that the sum is equals to the inputed integer. If there are several pairs, you can output any pair. For example, if the input array is [1,2,4,5,7,11,15] and an integer 15, because 4 + 11 = 15, hence output 4 and 11.

analysis and solution

We try to figure out this problem step by step. (we should notice the difference of ordered and unordered.)

The simle method is to list all the conditions. Namely select two numbers from the array to judge whether equals to the two numbers. The time complexity is O(n^2). Obviously, we need find a more efficient method.

method1: hash map

If the problem has a serious requrement for the time complexity, we may consider “exchange time with room”. Namely, for the given number, we can whether the other number exists in the array. The time complexity is O(1) and need to query n times, hence the total time complexity is O(n). But the preqeruirement is that we need an O(n) room to construct the hash map.

method2: sorting and move to the middle

If the array is unordered, we should make it ordered firstly. For example, for each a[i], find whether sum - a[i] exists in the original array, and we can use binary search to find sum - arr[i], and it needs long time. Hence, added with the sorting time complexity, the total complexity is O(nlogn + nlogn) = O(nlogn), and the room complexity is O(1).

If the array is ordered, let two pointers begin and end to point to the begin and end of the array. Let begin = 0, end = n -1, and begin++, end—, and judge if a[begin]+a[end] equals to the given sum.

- When a[begin] + a[end] > sum, we need to let the value of a[begin] + a[end] decrease. Hence begin will not change, and end—.

- When a[begin] + a[end]sum, we need to increase the value of a[begin] + a[end]. Hence end will not change, and begin++.

function twoSum(a, sum) {
var begin = 0;
var len = a.length;
var end = len - 1;
while (begin < end) {
var curSum = a[begin] + a[end];
if (curSum === sum) {
console.log(a[begin] + ' ' + a[end]);
break;
} else {
if (curSum < sum) {
begin ++;
} else {
end --;
}
}
}
}

the sum of two fixed value的更多相关文章

  1. 【POJ1707】【伯努利数】Sum of powers

    Description A young schoolboy would like to calculate the sum for some fixed natural k and different ...

  2. [伯努利数] poj 1707 Sum of powers

    题目链接: http://poj.org/problem?id=1707 Language: Default Sum of powers Time Limit: 1000MS   Memory Lim ...

  3. [CLR via C#]16. 数组

    数组是允许将多个数据项当作一个集合来处理的机制.CLR支持一维数组.多维数组和交错数据(即由数组构成的数组).所有数组类型都隐式地从System.Array抽象类派生,后者又派生自System.Obj ...

  4. C++求平均数

    题目内容:求若干个证书的平均数. 输入描述:输入数据含有不多于5组的数据,每组数据由一个整数n(n<=50)打头,表示后面跟着n个整数. 输出描述:对于每组数据,输出其平均数,精确到小数点后3位 ...

  5. 北大ACM(POJ1004-Financial Management)

    Question:http://poj.org/problem?id=1004问题点:求平均值及格式化输出. Memory: 248K Time: 0MS Language: C++ Result: ...

  6. 【南阳OJ分类之语言入门】80题题目+AC代码汇总

    小技巧:本文之前由csdn自动生成了一个目录,不必下拉一个一个去找,可通过目录标题直接定位. 本文转载自本人的csdn博客,复制过来的,排版就不弄了,欢迎转载. 声明: 题目部分皆为南阳OJ题目. 代 ...

  7. 2013腾讯编程马拉松初赛第〇场(3月20日)湫湫系列故事——植树节 HDOJ 4503

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=4503 思路:hint from a GOD-COW. 将每一个人模拟成图的一个点,两点连线当且仅当两人是朋 ...

  8. Financial Management POJ - 1004

    Financial Management POJ - 1004 解题思路:水题. #include <iostream> #include <cstdio> #include ...

  9. noip第6课作业

    1.    数据统计 [问题描述] 输入N个整数,求出它们的最小值.最大值和平均值(保留3位小数).输入保证这些数都是不超过1000的整数.(1<=N<=1000) [样例输入] 8 2 ...

随机推荐

  1. 原生js控制控制--弹窗的显示和隐藏

    以防浪费大家的时间,还是先上效果图吧,满足您的需求就往下look吧. 重要知识点:点击其他地方,也就是除了小叉子之外的地方也能够关闭弹窗哦.代码已标红    html代码: <button id ...

  2. Leetcode 16. 3Sum Closest(指针搜索)

    16. 3Sum Closest Medium 131696FavoriteShare Given an array nums of n integers and an integer target, ...

  3. python 获取list某个元素下标

    index() 函数用于从列表中找出某个值第一个匹配项的索引位置. list.index(x, start, end) #start end 指示搜索的起始和结尾位置,缺省为整个数组 x-- 查找的对 ...

  4. leetcode-mid-array-31 three sum-NO

    my code:    time limited def threeSum(nums): """ :type nums: List[int] :rtype: List[L ...

  5. 【洛谷P1310 表达式的值】

    题目链接 题目描述 对于1 位二进制变量定义两种运算: 运算的优先级是: 先计算括号内的,再计算括号外的. “× ”运算优先于“⊕”运算,即计算表达式时,先计算× 运算,再计算⊕运算.例如:计算表达式 ...

  6. 如何让ls按目录和文件 分开进行列表?

    linux的思想是: 有很多 "小工具", 但是功能并不弱的 小工具, 组合起来完成一些复杂的工作, 通过 这些工具的组合可以完成各种各样的, 不同的任务. 如: ls, sort, hea ...

  7. fiddler模拟弱网操作

    弱网是app测试需要覆盖的一种场景 目录 1.认识弱网 2.fiddler模拟弱网配置 3.弱网下可能发生的问题 1.认识弱网 弱网.2G.3G建议的上下行速率如下,同时还可以控制丢包率的数据 网络 ...

  8. prometheus linux系统告警规则 实例

    #prometheus linux系统告警规则 实例 #根据实际情况修改参数 #rules.linux.yml groups: - name: linux rules: - alert: Node-D ...

  9. linux 软连接的使用

    软连接是linux中一个常用命令,它的功能是为某一个文件在另外一个位置建立一个同不的链接. 具体用法是:ln -s 源文件 目标文件. 当 我们需要在不同的目录,用到相同的文件时,我们不需要在每一个需 ...

  10. idea注册

    1:要先得到一个注册码 http://idea.lanyus.com/ 2:之后需要在引导下 修改文件 使用前请将“0.0.0.0 account.jetbrains.com”添加到hosts文件中