查找等差数列中的缺失项.

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 等差数列缺失项的更多相关文章

  1. 【leetcode】1228.Missing Number In Arithmetic Progression

    题目如下: 解题思路:题目很简单.先对数组排序,根据最大值和最小值即可求出公差,然后遍历数组,计算相邻元素的差,如果差不等于公差,即表示数字缺失. 代码如下: class Solution(objec ...

  2. POJ3495 Bitwise XOR of Arithmetic Progression

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 772   Accepted: 175 Description Write ...

  3. CF 1114 E. Arithmetic Progression

    E. Arithmetic Progression 链接 题意: 交互题. 有一个等差序列,现已打乱顺序,最多询问60次来确定首项和公差.每次可以询问是否有严格大于x的数,和查看一个位置的数. 分析: ...

  4. 【LeetCode】413. Arithmetic Slices 等差数列划分

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 双指针 递归 动态规划 日期 题目地址:htt ...

  5. 268. Missing Number -- 找出0-n中缺失的一个数

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. 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,问是否在每个数 ...

  7. codeforces C. Arithmetic Progression 解题报告

    题目链接:http://codeforces.com/problemset/problem/382/C 题目意思:给定一个序列,问是否可以通过只插入一个数来使得整个序列成为等差数列,求出总共有多少可能 ...

  8. Dirichlet's Theorem on Arithmetic Progression

    poj3006 Dirichlet's Theorem on Arithmetic Progressions 很显然这是一题有关于素数的题目. 注意数据的范围,爆搜超时无误. 这里要用到筛选法求素数. ...

  9. CF1114E Arithmetic Progression(交互题,二分,随机算法)

    既然是在CF上AC的第一道交互题,而且正是这场比赛让我升紫了,所以十分值得纪念. 题目链接:CF原网 题目大意:交互题. 有一个长度为 $n$ 的序列 $a$,保证它从小到大排序后是个等差数列.你不知 ...

随机推荐

  1. 【Java EE 学习 28 上】【oracle学习第二天】【子查询】【集合运算】【几种数据库对象】

    一.子查询 1.为什么要使用子查询:问题不能一步求解或者一个查询不能通过一步查询得到. 2.分类:单行子查询和多行子查询. 3.子查询的本质:一个查询中包含了另外一个或者多个查询. 4.使用子查询的规 ...

  2. Linux下安装JDK多种方式

    一.环境说明: 操作系统:Linux xx- -.el6.x86_64 # SMP Thu Jul :: UTC x86_64 x86_64 x86_64 GNU/Linux jdk版本:java-- ...

  3. Redis相关

    Redis 持久化 1 why 数据需要持久化,当内存数据库使用的情况 防止缓存失效时候的雪崩效应 2 how 两种方式,快照和日志(aof)方式,各有优缺点. Redis的缓存失效策略 1 what ...

  4. vue

    vue.js 插件 setting--> plugins 搜索vue,下载安装如果想要高亮显示*.vue文件,可以在File Types 选项里找到HTML,然后在下方手动添加*.vue,这样就 ...

  5. Intellij IDEA中的Mybatis Plugin破解

    具体的破解过程请看:https://github.com/luyanliang/profile/blob/master/idea/plugin/MybatisPlugin/Mybatis-Plugin ...

  6. 基于hk2框架的功能测试Mock注入

    public Object getInstance(Class<?> clz){ return IocBean.get(clz.getName()); } public Object Mo ...

  7. EF MySql 配置文件

    <?xml version="1.0" encoding="utf-8"?><!--有关如何配置 ASP.NET 应用程序的详细信息,请访问 ...

  8. 对Ajax连接的认识~为毛不能上传文件!!!

    最近做毕设的时候需要用到上传图片的功能,但是我的毕设全部的传输都是基于ajax的请求,百度了一圈发现TMD居然说ajax不能上传文件!!当时我就不乐意了啊,那难道其他人都用的是黑科技吗?!又来网上的大 ...

  9. db2 游标使用

    游标一般用来迭代结果集中的行 为了在一个过程中处理一个游标的结果,需要做以下事情: 在存储过程块的开头部分 DECLARE 游标. 打开该游标. 将游标的结果取出到之前已声明的本地变量中(隐式游标处理 ...

  10. vs 2015 连接不上tfs 错误代码:TF31002

    在vs2015里面怎么也连接不上,把地址放到浏览器里可以打开,所以点击右边的 在visual studio 中打开,然后将源码映射到本地