
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum sum and product of the M consecutive digits in a number in C++
In this problem, we are given a string denoting a number. Our task is to create a program to find the Maximum sum and product of the M consecutive digits in a number in C++.
Problem Description
We find all sequences of M consecutive digits. And return the maximum sum and product.
Let’s take an example to understand the problem,
Input
number = 2379641, M = 4
Output
maxSum = 26maxProd = 1512
Explanation
All subsequences of size 4 are 2379, 3796, 7964, 9641. maxSum = 7 + 9 + 6 + 4 = 26 maxProd = 7 * 9 * 6 * 4 = 1512
Solution Approach
A simple solution to the problem is find all possible consecutive subsequences of size M form the number. Then add and multiply all values of the integer and then return the maximum of all sum and product values.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int findMaxVal(int x, int y){ if(x > y) return x; return y; } void calcMaxProductAndSum(string number, int M){ int N = number.length(); int maxProd = -1, maxSum = -1; int product = 1, sum = 0; for (int i = 0; i < N - M; i++){ product = 1, sum = 0; for (int j = i; j < M + i; j++){ product = product * (number[j] - '0'); sum = sum + (number[j] - '0'); } maxProd = findMaxVal(maxProd, product); maxSum = findMaxVal(maxSum, sum); } cout<<"The Maximum Product of "<<M<<" consecutive digits in number "<<number<<" is "<<maxProd<<endl; cout<<"The Sum Product of "<<M<<" consecutive digits in number "<<number<<" is "<<maxSum; } int main() { string str = "2379641"; int m = 4; calcMaxProductAndSum(str, m); }
Output
The Maximum Product of 4 consecutive digits in number 2379641 is 1512 The Sum Product of 4 consecutive digits in number 2379641 is 26
- Related Articles
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- Difference between product and sum of digits of a number in JavaScript
- Product sum difference of digits of a number in JavaScript
- Consecutive element maximum product in Python
- A two-digit number is 4 times the sum of its digits and twice the product of the digits. Find the number.
- A two digit number is 4 times the sum of its digits and twice the product of its digits. Find the number.
- Subtract the Product and Sum of Digits of an Integer in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Finding product of Number digits in JavaScript
- Find smallest number with given number of digits and sum of digits in C++
- What is the minimum and maximum number of digits in the sum if we add any two 3 digit number
- Prime digits sum of a number in JavaScript
- Maximum sum of n consecutive elements of array in JavaScript
- Destructively Sum all the digits of a number in JavaScript

Advertisements