{"id":"4b03f5bf-a4ef-4381-b44a-1dea475f099d","name":"Consecutive Numbers Sum","description":"1. Given an integer n.\r\n2. Return the number of ways you can write n as the sum of consecutive positive integers.\r\n","inputFormat":"1. n = 5\r\n2. n = 9\r\n3. n = 15","outputFormat":"1). 2\r\nExplanation: 5 = 2 + 3\r\n2). 3\r\nExplanation: 9 = 4 + 5 = 2 + 3 + 4\r\n3). 4\r\nExplanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5\r\n","constraints":"1. 1 &lt;= n &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint consecutiveNumbersSum(int n) {\n // write your code here\n return 0;\n}\n\nint main() {\n int num = 0;\n cin>>num;\n int res = consecutiveNumbersSum(num);\n cout<<res;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~User''s Section~~~~~~~~~~~~~\r\n public static int consecutiveNumbersSum(int n) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~Input Management~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int num = scn.nextInt();\r\n int res = consecutiveNumbersSum(num);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"9fa79ae9-f221-46da-869b-e9b7f5a0ea15","name":"Consecutive Numbers Sum","slug":"consecutive-numbers-sum","type":1}],"next":{"id":"256741c4-196e-46e8-875b-75c9216960fb","name":"Consecutive Numbers Sum","type":3,"slug":"consecutive-numbers-sum"},"prev":{"id":"551809ba-58cd-45e0-9028-33965deeec32","name":"Push Dominoes MCQ","type":0,"slug":"push-dominoes-mcq"}}}

Consecutive Numbers Sum

1. Given an integer n. 2. Return the number of ways you can write n as the sum of consecutive positive integers.

{"id":"4b03f5bf-a4ef-4381-b44a-1dea475f099d","name":"Consecutive Numbers Sum","description":"1. Given an integer n.\r\n2. Return the number of ways you can write n as the sum of consecutive positive integers.\r\n","inputFormat":"1. n = 5\r\n2. n = 9\r\n3. n = 15","outputFormat":"1). 2\r\nExplanation: 5 = 2 + 3\r\n2). 3\r\nExplanation: 9 = 4 + 5 = 2 + 3 + 4\r\n3). 4\r\nExplanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5\r\n","constraints":"1. 1 &lt;= n &lt;= 10^9\r\n","sampleCode":{"cpp":{"code":"#include <bits/stdc++.h>\nusing namespace std;\n\nint consecutiveNumbersSum(int n) {\n // write your code here\n return 0;\n}\n\nint main() {\n int num = 0;\n cin>>num;\n int res = consecutiveNumbersSum(num);\n cout<<res;\n}"},"java":{"code":"import java.util.*;\r\n\r\npublic class Main {\r\n\r\n // ~~~~~~~~~~~User''s Section~~~~~~~~~~~~~\r\n public static int consecutiveNumbersSum(int n) {\r\n // write your code here\r\n }\r\n\r\n // ~~~~~~~~~Input Management~~~~~~~~~~~~~~\r\n public static void main(String[] args) {\r\n Scanner scn = new Scanner(System.in);\r\n int num = scn.nextInt();\r\n int res = consecutiveNumbersSum(num);\r\n System.out.println(res);\r\n }\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5","sampleOutput":"2","questionVideo":"","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"35f2cfb0-6f25-4967-b0c9-92f2384b9260","name":"Arrays And Strings For Intermediate","slug":"arrays-and-strings-for-intermediate-732","type":0},{"id":"9fa79ae9-f221-46da-869b-e9b7f5a0ea15","name":"Consecutive Numbers Sum","slug":"consecutive-numbers-sum","type":1}],"next":{"id":"256741c4-196e-46e8-875b-75c9216960fb","name":"Consecutive Numbers Sum","type":3,"slug":"consecutive-numbers-sum"},"prev":{"id":"551809ba-58cd-45e0-9028-33965deeec32","name":"Push Dominoes MCQ","type":0,"slug":"push-dominoes-mcq"}}}
plane

Editor


Loading...

Consecutive Numbers Sum

hard

1. Given an integer n. 2. Return the number of ways you can write n as the sum of consecutive positive integers.

Constraints

1. 1 <= n <= 10^9

Format

Input

1. n = 5 2. n = 9 3. n = 15

Output

1). 2 Explanation: 5 = 2 + 3 2). 3 Explanation: 9 = 4 + 5 = 2 + 3 + 4 3). 4 Explanation: 15 = 8 + 7 = 4 + 5 + 6 = 1 + 2 + 3 + 4 + 5

Example

Sample Input

5

Sample Output

2

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode