TechoSagar: Maximum Integer Value

Search

Google Alert - jobs

Maximum Integer Value

Company:  Facebook
Given a string of numbers, the task is to find the maximum value from the string, you can add a ‘+’ or ‘*’ sign between any two numbers.
Note: Add sign between two numbers on the basis of numbers not on the value calculated till that number.
For eg: n = 5120    
(((5  + 1 )  + 2 )  + 0 ) = 8 is right.
5120   
(((5  + 1 )  * 2 )  + 0 ) =  12 is wrong.
Input:
First line of input contains number of testcases.
Each testcase has a line of input contains a string.

Output:
Print the maximum value obtained from the string.Value can be very large,so print using modulo 10^9+7.

Constraints:
1<=T<=10
1<=length of string<=10^6
0<=number<=9

Example:
Input:
01231
891
Output:
10
73
Explanation:
For testcase 1: ((((0 + 1) + 2) * 3) + 1) = 10
In above manner, we get the maximum value i.e. 10
For testcase 2: As 8*9*1 = 72 and 8*9+1 = 73.
So, 73 is maximum.
Get Your Code
#include<iostream>

using namespace std;
#include<sstream>
int convert(string p)
{
stringstream geek(p);
int x;
geek>>x;
return x;
}
int main()
{
int t;
cin>>t;
while(t--)
{
string s;
cin>>s;
int l=s.length();
string w;
w=s[0];
int i,sum=convert(w);
   
//int k=0;
char c;
for(i=0; i<l-1; i++)
{    
if(s[i]=='0'|| s[i]=='1')
{
c='+';
}
else
{
if(s[i+1]=='0'|| s[i+1]=='1')
{
c='+';
}
else
c='*';
}
//cout<<c<<endl;
w=s[i+1];
int y=convert(w);
// cout<<y<<endl;
if(c=='*')
{
sum=sum*y;
}
else
{
sum=sum+y;
}
}
cout<<sum<<endl;
}
}

Follow us

Follow Bijendra Kumar on Facebook