Easy Math
Chef is attending math classes. On each day, the teacher gives him homework. Yesterday, the teacher gave Chef a sequence of positive integers and asked him to find the maximum product of two different elements of this sequence. This homework was easy for Chef, since he knew that he should select the biggest two numbers.
However, today, the homework is a little bit different. Again, Chef has a sequence of positive integers , but he should find two different elements of this sequence such that the sum of digits (in base ) of their product is maximum possible.
Chef thought, mistakenly, that he can still select the two largest elements and compute the sum of digits of their product. Show him that he is wrong by finding the correct answer ― the maximum possible sum of digits of a product of two different elements of the sequence .
Input
- The first line of the input contains a single integer denoting the number of test cases. The description of test cases follows.
- The first line of the input contains a single integer .
- The second line contains space-separated integers
Output
For each test case, print a single line containing one integer ― the maximum sum of digits.
Constraints
- for each valid
Subtasks
Subtask #1 (100 points): original constraints
Example Input
3
2
2 8
3
8 2 8
3
9 10 11
Example Output
7
10
18
Explanation
Example case 1: The only two numbers Chef can choose are and . Their product is and the sum of digits of is .
Example case 2: Chef can choose and ; their product is . Note that it is allowed to choose two different elements with the same value.
Example case 3: Chef can choose and . Their product is and the sum of its digits is . Note that choosing and will result in a larger product (), but the sum of its digits is just
Solution using c++ language:-
#include <iostream>
#define ll long long int
using namespace std;
int sod(int n)
{
int sum=0;
while(n!=0)
{
sum+=n%10;
n=n/10;
}
return sum;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int s,max=0;
ll p;
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
p=a[i]*a[j];
s=sod(p);
if(s>max)
max=s;
}
}
cout<<max<<"\n";
}
// your code goes here
return 0;
}
Reddit homework help Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me.
ReplyDelete