0
$\begingroup$

I know its simple problem, but i want to know if it can be solved some other way around.Here goes the problem.

Input: 234 , 54678 , 34987

Problem is to calculate how many of the numbers(by number i mean the comma separated number not single digit) has an average greater than 2.Sure we can sum up digit's of each comma separated numbers and then calculate the average by dividing it by its count. But i want to know if there is some other way of doing it which skips summing up the digits and dividing it by its count.

$\endgroup$
1
  • 1
    $\begingroup$ The average is no smaller than the smallest digit. $\endgroup$ Commented Dec 23, 2011 at 16:30

2 Answers 2

1
$\begingroup$

You could use the "give and take" method. Given a sequence of digits you can subtract $a$ from one digit and add $a$ to another. This leaves the average the same.

Take from the large digits and add to the small digits. Repeat until you see that the average is greater than two (all digits greater than or equal to 2 and one greater than 2) or less than or equal to 2 (all digits less than or equal to 2).

Example: $$ \color{maroon}1 \ \ 4\ \ \color{maroon}{6}\ 1 \ \ 2\ \ \color{green}{9}\ \ \color{green}1\ \ 1 \ \ 1 $$ $$ \Downarrow $$ $$ \color{maroon}4 \ \ 4\ \ \color{maroon}{3}\ 1 \ \ 2\ \ \color{green}{5}\ \ \color{green}5\ \ 1 \ \ 1 $$ Then $$ \color{orange} 4 \ \ 4\ \ 3\ \ \color{orange}1 \ \ 2\ \ \color{maroon} 5\ \ \color{green}5\ \ \color{maroon}1 \ \ \color{green}1 $$ $$ \Downarrow $$ $$ \color{orange}3 \ \ 4\ \ 3\ \ \color{orange}2 \ \ 2\ \ \color{maroon} 3\ \ \color{green}3\ \ \color{maroon}3 \ \ \color{green}3 $$

We see the average is greater than 2.

$\endgroup$
0
$\begingroup$

There are several tricks which may help in some cases: for example for $54678$ where each digit is greater than $2$ and so their average is.

But there will be other cases such as $1112070133$ and $1112090133$ where I expect summing the digits and dividng by the number of digits is going to be one of the easiest options.

If you really hate division or counting numbers of digits, then you can subtract $2$ from each digit before adding up, so the calculation for $1112070133$ becomes $-1-1-1+0-2+5-2-1+1+1 = -1$ which being negative shows that the digit average is below $2$.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .