Problem of the day
You are given a positive integer 'N.' Your task is to find the number of ways to represent the given number 'N' as the sum of two or more consecutive natural numbers.
The first line contains an integer, ‘T,’ which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first and the only line of each test case contains one integer 'N', as described in the problem statement.
Output Format:
For each test case, return one integer denoting the number of ways to represent the given number 'N' as the sum of 2 or more consecutive natural numbers.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 100
1 <= N <= 10^8
Time Limit: 1 sec
1
9
2
9 can be represented as (2+3+4 = 9) and (4+5 = 9).
1
8
0
There is no way to represent 8 in the form of a sum of 2 or more consecutive natural numbers.
Think of a brute force approach.
Approach:
In this brute force approach,. We will iterate from 1 to ‘N’ using the iterator i and will check if there is a possible sequence of consecutive positive integers such that their sum is ‘N’, and the ending integer of the consecutive sequence is 'i'. The checking will be done as follows:
Steps:
O(N), where ‘N’ is the given number.
We are iterating from 1 to ‘N’, and finding the root of the equation takes constant time. Hence, the time complexity is O(N).
O(1).
Since constant extra space is required.