DEV Community

Cover image for Day 17 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#829. Consecutive Numbers Sum(Hard/JavaScript)
KillingLeetCode
KillingLeetCode

Posted on • Updated on

Day 17 of Studying LeetCode Solution until I Can Solve One on My Own: Problem#829. Consecutive Numbers Sum(Hard/JavaScript)

Intro: I am a former accountant turned software engineer graduated from coding bootcamp in January 2022. Algorithms and Data Structure is an unavoidable part of interviews for most of the tech companies now. And one of my friends told me that you need to solve a medium leetcode problem under 60 seconds in order to get into the top tech companies.So I thought I'd start learning how to do it while job searching.

Since I have no clue on how to solve any of the problems (even the easy ones), I thought there is no point for me to waste hours and can't get it figured out. Here is my approach:

  • Pick a leetcode problem randomly or Online Assessment from targeted companies.
  • Study 1-2 solutions from Youtube or LeetCode discussion section. One brute force solution, another one more optimal.
  • Write a blog post with detailed explanation and do a verbal walk through to help understand the solutions better.
  • Code out the solution in LeetCode without looking at the solutions
  • Combat the forgetting curve: Re-do the question for the next three days. And come back regularly to revisit the problem.

829. Consecutive Numbers Sum
Difficulty: Hard Language: JavaScript

Another math problem

Given an integer n, return the number of ways you can write n as the sum of consecutive positive integers.

Example 1:

Input: n = 5
Output: 2
Explanation: 5 = 2 + 3
Enter fullscreen mode Exit fullscreen mode

Example 2:

Input: n = 9
Output: 3
Explanation: 9 = 4 + 5 = 2 + 3 + 4
Enter fullscreen mode Exit fullscreen mode

Example 3:

Input: n = 15
Output: 4
Explanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
Enter fullscreen mode Exit fullscreen mode

Constraints:

  • 1 <= n <= 109

Solution:
I found three difference solutions and below solution has less code, is easy to understand/explain and it runs faster and takes less space. I attached youtube link in the reference section. Key to this problem is to find a pattern in the numbers like all other Math problems on LeetCode. To further analyze example 3 above, one of the combination is 4 + 5 + 6. We can break it down to:
(3 + 1) + (3 + 2) + (3 + 3) = 15. The set exists when (15 - 1 - 2 - 3) is divisible by 3 has no remainder. With this pattern, we will keep subtracting 4,5...i from 15 until n = 1. Whenever remaining n is divisible by i, there is a working ways you can write 15 as the sum of consecutive positive integers

var consecutiveNumbersSum = function (n) {

    let count = 0, i = 1;

    while (n > 0){

//while (note 1) n is greater than 0

        n-=i;

//subtract i from n (note 2)

        if(n % i ==0) 

//if (n-i) is divisible (note 4) by i, a working combinaion is found

        count ++;
        i++;

//increase (note 3) count to record numbers of ways found to write
//n as the sum of consecutive positive integers. Increase i by 1
//to test out next possibility.

    }
    return count
};
Enter fullscreen mode Exit fullscreen mode

Solution Submission detail as of 2/27/2022
(Data below could vary since there are new tests/submissions daily)

  • Runtime: 91 ms
  • Memory Usage: 41.9 mb

References:
LeetCode Problem Link
Youtube: Aslan Tashtanov
Note 1: While loop
Note 2: Subtraction assignment (-=)
Note 3: Increasement(++)
Note 4: Remainder(%)
Blog Cover Image Credit

Top comments (0)