Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers?

Example 1:

Input: 5
Output: 2
Explanation: 5 = 5 = 2 + 3

Example 2:

Input: 9
Output: 3
Explanation: 9 = 9 = 4 + 5 = 2 + 3 + 4

Example 3:

Input: 15
Output: 4
Explanation: 15 = 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5

Note: 1 <= N <= 10 ^ 9.

Approach #1: Math. [Java]

class Solution {
public int consecutiveNumbersSum(int N) {
int count = 1;
for (int k = 2; k < Math.sqrt(2*N); ++k) {
if ((N - (k * (k - 1) / 2)) % k == 0)
count++;
}
return count;
}
}

  

Analysis:

The though process goes like this - Given a number N, we can possibly write it as a sum of 2 numbers, 3 numbers, 4 numbers and so on. Let's assum the first number in this serise be x. Hence, we should have:

x + (x + 1) + (x + 2) + ... + (x + k) = N

kx + k * (k - 1) / 2 = N

kx = N - k * (k - 1) / 2

So, we can calculate the RHS for every value of k and if it is a multiple of k then we can construct a sum of N using k terms string from x.

Now, the question arises, till what value of k should we loop for?

That's easy. In the worst case, RHS should be greater than 0, That is

N - k * (k - 1) / 2 > 0

k * (k - 1) < 2 * N

~ k * k < 2N ==> k < sqrt(2N)

Hence the overall complexity of the algorithm is O(sqrt(N)).

Reference:

https://leetcode.com/problems/consecutive-numbers-sum/discuss/129015/5-lines-C%2B%2B-solution-with-detailed-mathematical-explanation.

829. Consecutive Numbers Sum的更多相关文章

  1. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  2. 【LeetCode】829. Consecutive Numbers Sum 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学方法 日期 题目地址:https://leetc ...

  3. [Swift]LeetCode829. 连续整数求和 | Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  4. Consecutive Numbers Sum

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  5. leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

    一.Nth Highest Salary Write a SQL query to get the nth highest salary from the Employee table. +----+ ...

  6. LeetCode Database: Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  7. [LeetCode] Consecutive Numbers 连续的数字 --数据库知识(mysql)

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  8. 【leetcode】1296. Divide Array in Sets of K Consecutive Numbers

    题目如下: Given an array of integers nums and a positive integer k, find whether it's possible to divide ...

  9. [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers

    SQL架构: Create table If Not Exists Logs (Id int, Num int) Truncate table Logs insert into Logs (Id, N ...

随机推荐

  1. WPF窗口和用户控件事件相互触发

    问题1: WPF项目里有一个窗口和一个用户控件,窗口和用户控件里都有一个Button,点击窗口里的Button如何触发用户控件里Button的Click事件 解答: //窗口代码 public par ...

  2. 500GJava/Hadoop/Spark/机器学习...视频教程免费分享 百度云持续更新

    参加工作这么长时间了,工作中遇到了不少技能都是看视频教程学习的,相比较看书而言看视频确实比较容易理解.分享一下自己看过的和收集的视频教程. 资源包括: 大数据方面的Hadoop(云帆,小象学院,八斗学 ...

  3. Caffe介绍与测试及相关Hi35xx平台下caffe yolox的使用参考

    这一篇我大概讲讲Caffe框架下MNIST的实现与基于Hi35xx平台下caffe yolox的运用等,供大家参考 1.Caffe介绍与测试 caffe全称Caffe Convolutional Ar ...

  4. AtCoder Beginner Contest 194

    A I Scream int main() { IOS; int a, b; cin >> a >> b; if(a + b >= 15 && b > ...

  5. django框架如何解决跨域问题

    跨域问题的由来 由于浏览器具有同源策略的限制. 限制:在发送Ajax请求时,如果当前浏览器的URL是a.com,页面中向b.com发送Ajax请求,请求可以正常访问,但数据回到浏览器时,浏览器会阻止. ...

  6. Java8 BiFunction 简单用用

    最近来了新公司,主要用到了ElasitcSearch,大家都知道在底层查询代码中往往需要判断传入某个参数是否为空来判断设置查询,例如下方代码: BoolQueryBuilder query = Que ...

  7. 程序员必须搞懂的20个Java类库和API

    本文总结了日志.JSON解析.单测.XML解析.字节码处理.数据库连接池.集合类.邮件.加密.嵌入式SQL数据库.JDBC故障诊断以及序列化等20个方面的常用类库.都是你日常开发经常可能要用到的,现在 ...

  8. salesforce lightning零基础学习(十七) 实现上传 Excel解析其内容

    本篇参考: https://developer.mozilla.org/zh-CN/docs/Web/API/FileReader https://github.com/SheetJS/sheetjs ...

  9. python-实现双链表

    双链表和单链表进行比较的优点与不同 节点多了一个前驱指针域 在很多基本操作上,多了一种选择,因为双链表可以向前进行移动寻位 如果给每个节点添加一个对应的下标,那么在寻找节点时,我们可以使用二分发来进行 ...

  10. 4、MyBatis教程之配置解析

    5.配置解析 核心配置文件 mybatis-config.xml 系统核心配置文件 MyBatis 的配置文件会深深影响 MyBatis 行为的设置和属性信息. 能配置的内容如下: configura ...