Break Numbers[Duplicate problem] | Practice | GeeksforGeeks

Given a large number N, write a program to count the ways to break it into three whole numbers such that they sum up to the original number.
Example 1:
Input: N = 3
Output: 10
Explaination: The ways are 
0 + 0 + 3 = 3
0 + 3 + 0 = 3
3 + 0 + 0 = 3
0 + 1 + 2 = 3
0 + 2 + 1 = 3
1 + 0 + 2 = 3
1 + 2 + 0 = 3
2 + 0 + 1 = 3
2 + 1 + 0 = 3
1 + 1 + 1 = 3
Example 2:
Input: N = 1
Output: 3
Explaination: The ways are:
1 + 0 + 0 = 1
0 + 1 + 0 = 1
0 + 0 + 1 = 1
Your Task:
You do not need to read input or print anything. Your task is to complete the function countWays() which takes N as input parameter and returns the total number of possible ways to break N in three whole numbers.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 109  
0
sudarshanp49121 month ago
For explanation must read bar and stars technique (permutation and combination chapter of 12th).
 
You can use this link to get more deeper understanding of the topic:
 
https://en.wikipedia.org/wiki/Stars_and_bars_(combinatorics)
0
vishwajeetofficial20223 months ago
//JAVA SOLUTION

class Solution{
    static long countWays(long N){
        // code here
         long l= (N+2)*(N+1)/2;
      
       return l;
    }
}
0
shaktig1011014 months ago
package string;
public class Greek {
 long countWays(long N) {             long l= (N+2)*(N+1)/2;       System.out.println(l);       return l;    }
public static void main(String[] args) { Greek obj=new Greek(); obj.countWays(3);}
} 
-1
hitentandon4 months ago
Java:
return (++N*++N)/2;
C++:
return ((++N)++*N)/2;
Python:
return int(((N+1)*(N+2))/2);
+1
badgujarsachin837 months ago
 long long countWays(long long N){
        // code here
    return ((N+2)*(N+1))/2;
    }
+1
itsjp7 months ago
It's simple permutation problem you have to understand stars and bars technique 
once you applied this algorithm then you'll find like there is multiplication of ((input+2)*(input+1))/2
 
long long countWays(long long N){
        // code here
        return ((N+2)*(N+1))/2;
    }
0
Addhayyan Anand8 months ago
Addhayyan Anand
PLEASE SOME ONE TELL ME WHATS WRONG IN THIS!!#StackOverflowError
class Solution{
    public static long fact(long n){        if(n == 0)            return 1;        return (n*fact(n-1));    }
    static long countWays(long N){        // code here        if(N <= 1){            return 3;        }
       long deno = 2*fact(N);      long nume = fact(N+2);
      return nume/deno;
    }}
0
MAYUR.xxivk9 months ago
MAYUR.xxivk
return (n+2)*(n+1)/2;
0
Abhishek Singh11 months ago
Abhishek Singh
class Solution{public:    long long countWays(long long N){        // code here        return ((N+2)*(N+1))/2;    }};
0
Vinay Kumar1 year ago
Vinay Kumar
class Solution:    def countWays(self, n):        # code here        return ((n+2)*(n+1))//2
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.