<p>Given an integer <code>n</code>, return <em>the number of ways you can write </em><code>n</code><em> as the sum of consecutive positive integers.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> 5 = 2 + 3
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 9
<strong>Output:</strong> 3
<strong>Explanation:</strong> 9 = 4 + 5 = 2 + 3 + 4
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 15
<strong>Output:</strong> 4
<strong>Explanation:</strong> 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution {
public:
    int consecutiveNumbersSum(int n) {
        // n
        // (a + (a + k - 1)) * k / 2 = (2a + k - 1)*k/2 = n;
        // 2n = (2a+k-1)*k
        // parity of k and (2a + k - 1) are opposite
        // 2a + k - 1 > k for all a in this problem
        int count = 0;
        for(int i = 1; i*i <= 2*n; i++) {
            if(2*n%i == 0) {
                int a = i;
                int b = 2*n / i;

                if((b - a) % 2 == 1) {
                    count++;
                }
            }
        }
        return count;
    }
};
7400