In the following interview question :
Given a number n, give me the numbers
(among3..5and an even number of
numbers) whose adding would return the
original number. The resulting numbers
should be as balanced as possible,
meaning that instead of returning3
and5, for instance, return4and
4. Ex:7 = 3 + 4 16 = 4 + 4 + 4 + 4 rather than 3 + 5 + 4 + 4 24 = 12 + 12 or 6 + 6 + 6 + 6
I thought of the following method:
splitnumber(int n)
{
//check if the number is even
if(n%2==0)
{
print(n/2,n/2);
//check if x=2^m multiple exists or
// not..like 4,8,16 etc
print (n/x...n/x);
}
else //else if the no is odd... this part is incomplete
{
if(n-3>0)
{
print (3);
}
n-=3;
if(n>0)
{
if (n>5)
{
print(3)
n-=3;
}
}
}
}
but still I am not able to complete all the cases… How should I check when the answer has unbalanced solution??
Slightly inefficient implementation in C#