Maximum sum and product of the M consecutive digits in a number | Java implementation of the above approach ; Function to find the maximum product ; Driver code
import java . io . * ; class GFG { static void maxProductSum ( String str , int m ) { int n = str . length ( ) ; int maxProd = Integer . MIN_VALUE , maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - m ; i ++ ) { int product = 1 , sum = 0 ; for ( int j = i ; j < m + i ; j ++ ) { product = product * ( str . charAt ( j ) - '0' ) ; sum = sum + ( str . charAt ( j ) - '0' ) ; } maxProd = Math . max ( maxProd , product ) ; maxSum = Math . max ( maxSum , sum ) ; } System . out . println ( " Maximum ▁ Product ▁ = ▁ " + maxProd ) ; System . out . print ( " Maximum Sum = " + maxSum); } public static void main ( String [ ] args ) { String str = "3605356297" ; int m = 3 ; maxProductSum ( str , m ) ; } }