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. 第28天学习打卡(Date和Calendar类 基本类型的包装类 集合 增强for循环 )

    Date和Calendar类 简介 日期和日历类,用于操作日期相关信息. 构造方法 Date(): 构造一个日期对象,当前系统时间,精确到毫秒. Date(long): 构造一个日期对象,时间为自&q ...

  2. Wireshark使用记录

    TCP/IP协议族里的协议众多 要一一精通比较困难,在一些紧急急需要分析主机.客户端的流量场景时,不懂协议也要上!下面就是用到哪里就记录到哪,有错误欢迎评论指出,多谢. wireshark这玩意相当于 ...

  3. java中ArrayList 和 LinkedList 有什么区别

    转: java中ArrayList 和 LinkedList 有什么区别 ArrayList和LinkedList都实现了List接口,有以下的不同点:1.ArrayList是基于索引的数据接口,它的 ...

  4. [msys2]集成到右键菜单

    集成到右键菜单 在资源管理器中,空白处右键(right-clicking on folder backround in Windows Explorer)会弹出菜单,其中有如"在此处打开cm ...

  5. HDOJ-2181(深搜记录路径)

    哈密顿绕行世界问题 HDOJ-2181 1.本题是典型的搜索记录路径的问题 2.主要使用的方法是dfs深搜,在输入的时候对vector进行排序,这样才能按照字典序输出. 3.为了记录路径,我使用的是两 ...

  6. .NET MVC & Web API Cors让AJAX 实现跨域

    什么是Cors? CORS是一个W3C标准,全称是"跨域资源共享"(Cross-origin resource sharing).它允许浏览器向跨源服务器,发出XMLHttpReq ...

  7. MMA CTF 2nd 2016-greeting

    目录 MMA CTF 2nd 2016-greeting 总结 题目分析 checksec 函数分析 漏洞点 知识点 利用思路 EXP 完整Exp MMA CTF 2nd 2016-greeting ...

  8. android分析之智能指针

    智能指针是一个包装类,该类有一个指针指向真正的类对象 引用计数型智能指针,该引用计数是在应该被真正类所持有,而非包装类(智能指针) 为了方便,会将引用计数单独实现在一个类中,这样所有继承它的类都有计数 ...

  9. Codeforces Round #575 (Div. 3) D2. RGB Substring (hard version) 【递推】

    一.题目 D2. RGB Substring (hard version) 二.分析 思路一开始就想的对的,但是,用memset给数组初始化为0超时了!超时了! 然后我按照题解改了个vector初始化 ...

  10. 前后端(PHP)使用AES对称加密

    前端代码: // 这个是加密用的 function encrypt(text){ var key = CryptoJS.enc.Utf8.parse('1234567890654321'); //为了 ...