- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Break Number Into 3 parts to find max sum
A number is given. Our task is to break the number three times by n/2, n/3, and n/4 and find maximum sum we can make by dividing the number into three parts.
For an example, 50 can be divided into {25, 16, 12}, now break each of the set {25, 16, 12}, into three divisions again, and so on. After completing the division up to 3 times, we will calculate the sum to find the maximum of them.
This program can be solved in a recursive way, but in the recursive approach, we need to find the same results for multiple times, so if we use the Dynamic programming approach and store the previously calculated data in a table, then it will reduce the time.
Input and Output
Input: Let the given number is 12. Output: The answer is 13. At first divide the 12 as (12/2 + 12/3 + 12/4) = 6 + 4 + 3 = 13. now divide 6 into three parts (6/2 + 6/3 + 6/4) = 3 + 2 + 1 = 6. If we divide the 4 and 3, we can get maximum 4 from them. From all values the maximum is 13.
Algorithm
maxBreakSum(n)
Input: The given number.
Output: Maximum sum after breaking.
Begin define sums array of size n+1 sums[0] := 0, sums[1] := 1 for i in range 2 to n, do sum[i] := maximum of i and (sums[i/2] + sums[i/3] + sums[i/d]) done return sums[n] End
Example
#include<iostream> #define MAX 1000000 using namespace std; int max(int a, int b) { return (a>b)?a:b; } int maxBreakSum(int n) { int sumArr[n+1]; sumArr[0] = 0, sumArr[1] = 1; //for number 0 and 1, the maximum sum is 0 and 1 respectively for (int i=2; i<=n; i++) //for number 2 to n find the sum list sumArr[i] = max(sumArr[i/2] + sumArr[i/3] + sumArr[i/4], i); //divide number by 2, 3, 4 return sumArr[n]; } int main() { int n; cout << "Enter a number: "; cin >> n; cout << "Maximum sum after breaking: " << maxBreakSum(n); }
Output
Enter a number: 12 Maximum sum after breaking: 13
- Related Articles
- Program to find max number of K-sum pairs in Python
- Program to find split a string into the max number of unique substrings in Python
- C++ Partition a Number into Two Divisible Parts
- Partition Array Into Three Parts With Equal Sum in Python
- Splitting number into n parts close to each other in JavaScript
- Split the array into equal sum parts according to given conditions in C++
- Divide a number into two parts in C++ Program
- Find the number of ways to divide number into four parts such that a = c and b = d in C++
- Divide 29 into two parts so that the sum of the squares of the parts is 425.
- C program to find sum, max and min with Variadic functions
- Ravi divided pizza into 6 equal parts and ate 3 parts from it. What is the fraction ?
- A circle is divided into certain number of equal parts. If 15 of the parts so formed represent the fraction ( frac{3}{5}, ) the number of parts in which the circle has been divided is(1) 45(2) 75(3) 20(4) 25
- C++ code to find final number after min max removal game
- Divide number into two parts divisible by given numbers in C++ Program
- How to divide an unknown integer into a given number of even parts using JavaScript?

Advertisements