Count number of ways to divide a number in 4 parts - GeeksforGeeks
Count number of ways to divide a number in 4 parts Given a positive integer n, find number of ways to divide n in four parts or represent n as sum of four positive integers. Here n varies from 0 to 5000. Examples: Input: n = 5 Output: 1 There is only one way (1, 1, 1, 2) Input: n = 6 Output: 2 There are two ways (1, 1, 1, 3) and (1, 1, 2, 2) Input: n = 8 Output: 5 There are five ways (2, 2, 2, 2), (1, 1, 1, 5), (1, 1, 3, 3), (1, 1, 2, 4) and (1, 2, 2, 3) We strongly recommend you to minimize your browser and try this yourself first. Method 1 (Simple Solution) Run four nested loops to generate all possible different quadruplet. Below is C++ implementation of the simple algorithm. // A Simple C++ program to count number of ways to // represent a number n as sum of four. #includeRead full article from Count number of ways to divide a number in 4 parts - GeeksforGeeks
No comments:
Post a Comment