Problem:Given a comma separated string of numbers, want to find out the highest average of digits of number.
for eg: consider a string of number “123,345,555” the output would be 5 since 5 is the highest average among 123,345,555.
Here is my program..in C#
int Max_Avg(string number_list)
{
int i;
Int32 sum;
Int32 max_avg = 0;
int limit = 0;
string[] num = number_list.Split(',');
limit = num.Length;
while (limit-- > 0)
{
i=0;
sum = 0;
while (i < num[limit].Length)
{
sum += Convert.ToInt32(num[limit].Substring(i, 1));
i++;
}
int tmp=sum / num[limit].Length;
if (tmp > max_avg)
{
max_avg = tmp;
}
}
return max_avg;
}
If any can one can optimize its performance or suggest a more optimised approach to speed it up …..
(optimized for maintainability / code, rather than raw performance)