- 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
Maximum value with the choice of either dividing or considering as it is in C++ program
In this problem, we are given a number N. Our task is to create a program to find to Maximum value with the choice of either dividing or considering as it is in C++.
Problem Description
To find the maximum, we can consider any two values, either by take the value as it is or we could get the maximum value by dividing.The value could be extracted as F(N/2) + F(N/3) + F(N/4) + F(N/5).
Let’s take an example to understand the problem,
Input:N = 8
Output:9
Explanation
F(8) =F(8/2) + F(8/3) + F(8/4) + F(8/5) = F(4) + F(2) + F(2) + F(1) = 4 + 2 + 2 + 1 = 9
Solution Approach
The idea is simply to call the same function multiple times for the value of division. For this we have used the concept dynamic programming and created an array to solve the values of F(i) from 0 to N, for recusing them for finding the solution.
Example
#include <iostream> using namespace std; int calcMaximumValue(int N) { int F[N + 1]; int divVal = 0; F[0] = 0; F[1] = 1; for (int i = 2; i <= N; i++) { divVal = ( F[i / 2] + F[i / 3] + F[i / 4] + F[i / 5] ); if(divVal > i) F[i] = divVal; else F[i] = i; } return F[N]; } int main() { int N = 8; cout<<"Maximum value with the choice of either dividing or considering as it is = "<<calcMaximumValue(N); return 0; }
Output
Maximum value with the choice of either dividing or considering as it is = 9
- Related Articles
- Maximum value with the choice of either dividing or considering as it is in C++
- Maximum length subarray with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 in C++
- Maximum length subsequence with difference between adjacent elements as either 0 or 1 | Set 2 in C++
- Maximum determinant of a matrix with every values either 0 or n in C++
- Problem with division as output is either 0 or 1 when using ifthenelse condition in ABAP program
- Maximum set bit sum in array without considering adjacent elements in C++
- Print pair with maximum AND value in an array in C Program.
- Delete leaf nodes with value as x in C++ Program
- C++ program to find out the maximum value of i
- Maximum sum after repeatedly dividing N by a divisor in C++
- C++ program to find maximum possible value for which XORed sum is maximum
- How far it is justifiable to change the name of cities, considering the financial cost it involves?
- C program to find out the maximum value of AND, OR, and XOR operations that are less than a given value
- On adding phenolphthalein indicator to colourless solution, no change is observed. What is the nature of this solution?(a) Basic(b) Either acidic or basic(c) Either acidic or neutral(d) Either basic or neutral
