Find Missing Term in Arithmetic Progression 等差数列缺失项
查找等差数列中的缺失项.
e.g.Input: arr[] = {2, 4, 8, 10, 12, 14}
Output: 6
Input: arr[] = {1, 6, 11, 16, 21, 31};
Output: 26.
采用binary search. 若是arr[mid] - arr[left] == (mid - left) * diff, 说明missing 部分在mid右侧.
否则missing部分在包括mid的左侧.
Time complexity: O(logn). Space: O(1).
public class Main {
public static void main(String[] args) {
try{
int [] arr1 = {2, 4, 8, 10, 12, 14};
int [] arr2 = {1, 6, 11, 16, 21, 31};
int [] arr3 = {1, 6};
System.out.println(findMissing(arr1));
System.out.println(findMissing(arr2));
System.out.println(findMissing(arr3));
}catch(IllegalArgumentException e){
System.out.println(e.getMessage());
}
}
private static int findMissing(int [] arr) throws IllegalArgumentException{
if(arr == null || arr.length < 3){
throw new IllegalArgumentException("Invalid input!");
}
int len = arr.length;
int l = 0;
int r = len-1;
int diff = (arr[r] - arr[l])/len;
System.out.println(diff);
while(l <= r){
int mid = l+(r-l)/2;
if(arr[mid] - arr[l] == (mid-l)*diff){
l = mid+1;
}else{
r = mid;
}
}
return arr[r] - diff;
}
}
Find Missing Term in Arithmetic Progression 等差数列缺失项的更多相关文章
- 【leetcode】1228.Missing Number In Arithmetic Progression
题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...
- POJ3495 Bitwise XOR of Arithmetic Progression
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 772 Accepted: 175 Description Write ...
- CF 1114 E. Arithmetic Progression
E. Arithmetic Progression 链接 题意: 交互题. 有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差.每次可以询问是否有严格大于x的数,和查看一个位置的数. 分析: ...
- 【LeetCode】413. Arithmetic Slices 等差数列划分
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...
- 268. Missing Number -- 找出0-n中缺失的一个数
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...
- codeforces C. Arithmetic Progression 解题报告
题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...
- Dirichlet's Theorem on Arithmetic Progression
poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...
- CF1114E Arithmetic Progression(交互题,二分,随机算法)
既然是在CF上AC的第一道交互题,而且正是这场比赛让我升紫了,所以十分值得纪念. 题目链接:CF原网 题目大意:交互题. 有一个长度为 $n$ 的序列 $a$,保证它从小到大排序后是个等差数列.你不知 ...
随机推荐
- LeetCode之237. Delete Node in a Linked List
------------------------------------------------ 因为不知道前序是谁,所以只好采用类似于数组实现的列表移动值, 又因为如果当前是最后一个元素了但是已经没 ...
- C实现栈和队列
这两天再学习了数据结构的栈和队列,思想很简单,可能是学习PHP那会没有直接使用栈和队列,写的太少,所以用具体代码实现的时候出现了各种错误,感觉还是C语言功底不行.栈和队列不论在面试中还是笔试中都很重要 ...
- 2.Java异常学习
1.Java异常的概念 异常的例子 1.除法就是一个需要捕获异常的例子,除数又可能是0 异常处理的基本流程如下 一旦发生异常,就使得程序不按照原来的流程继续的运行下去 a.程序抛出异常 try{ th ...
- Inno Setup 版本 5.5.3+ 简体中文语言包
; *** Inno Setup 版本 + 简体中文消息 *** [LangOptions] LanguageName=<7B80><4F53><> Languag ...
- 环境变量/path/classpath/JAVA_HOME/JAVA环境变量配置
环境变量 环境变量是在操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows和DOS操作系统中的path环境变量,当要求系统运行一个程序而没有告诉它程序所在 ...
- Android_ListView简单例子
ListView是Android软件开发中非常重要组件之一,基本上是个软件基本都会使用ListView ,今天我通过一个demo来教大家怎么样使用ListView组件 activity_main.xm ...
- nodejs review-02
30 Receive POST data POST接受JSON数据处理; //req. res都是可读的stream; http.createServer(function (req, res) { ...
- 破解加密PDF文件pdfcrack
破解加密PDF文件pdfcrack PDF是常见的文档格式.它允许用户设置双重密码来保护文档.第一重是用户密码(user password),当打开PDF文档,输入该密码.第二重是所有者密码(ow ...
- blur和click事件的先后顺序问题
这两个同时用会有冲突.懒得翻译了,需要的自己看哈. 以下摘自stackoverflow. I have an input field, where I try to make autocomplete ...
- 【Oracle】oracle10g以后利用q-quote特性简化包含单引号后双引号的字符串写法
The Q-quote delimiter can be any single- or multibyte character except space, tab, and return. If th ...