

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count number of ways to divide a number in parts in C++
We are given a positive number N. The goal is to count the number of ways in which the number N can be divided into 3 parts. The parts may or may not be equal. N lies in range [1,5000].
We will do this by using three for loops for 3 parts of the number. Check at the innermost loop that the sum of all three is equal to N. If true, then increment the count of ways.
Let’s understand with examples.
Input − N=5
Output − Number of ways to divide N in 3 parts: 2
Explanation − 5 can be shown as sum of (1,1,3) and (1,2,2)
Input − N=9
Output − Number of ways to divide N in 3 parts: 7
Explanation − 9 can be shown as sum of : (1, 1, 7), (1, 2, 6), (1, 3, 5), (1, 4, 4), (2, 2, 5), (2, 3,4) and (3, 3, 3).
Approach used in the below program is as follows
We take an integer N initialized with a value between 1 and 5000.
Function divideN(int n) takes n and returns the number of ways in which n can be divided into 3 parts.
Take the initial variable count as 0 for the number of ways.
Traverse using three for loops for each part of the number.
Outermost loop from 1<=i<n, inner loop i<=j<n , innermost j<=k<n.
Check if the sum of i, j and k is equal to n . If true then increment count.
At the end of all loops count will have a total number of ways to divide n in three parts.
Return the count as result.
Example
#include <bits/stdc++.h> using namespace std; int divideN(int n){ int count = 0; for (int i = 1; i < n; i++){ for (int j = i ; j < n; j++){ for (int k = j; k < n; k++){ int sum=i+j+k; if(sum==n) { count++; } } } } return count; } int main(){ int N=500; cout <<endl<< "Number of ways to divide N in 3 parts : "<<divideN(N); return 0; }
Output
If we run the above code it will generate the following output −
Number of ways to divide N in 3 parts: 20833
- Related Questions & Answers
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Divide a number into two parts in C++ Program
- All ways to divide array of strings into parts in JavaScript
- Find count of digits in a number that divide the number in C++
- Count the number of ways to traverse a Matrix in C++
- Count number of ways to reach a given score in a game
- Count number of ways to reach destination in a Maze in C++
- How to divide an unknown integer into a given number of even parts using JavaScript?
- Count ways to express a number as sum of powers in C++
- Count number of ways to jump to reach end in C++
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Divide number into two parts divisible by given numbers in C++ Program
- Divide a big number into two parts that differ by k in C++ Program
- Count number of ways to reach a given score in a Matrix in C++
- Count ways to spell a number with repeated digits in C++