Adobe Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
Start from an initial configuration like:
C=25, B=0, A=75
Keeping C fixed at 25, the configuration can be changed to the below in 51 steps
C=25, B=50, A=25
A change from configuration C=24, B=1, A=75 to C=24, B=50, A=26 is obtained in 50 steps. Continuing this way, a change from configuration C=0, B=25, A=75 to C=0, B=50, A=50 requires 26 steps.
Therefore the total number of configurations = 26 + 27 + ... + 51 = 1001.
public class CoinDistribution {
public static void main (String[] args) {
distribute();
}
public static void distribute() {
int c = 25, b = 0, a = 75, counter = 1, k = 0;
while(k <= 25) {
for (int j = k, d = 0; j <= 50; j++, d++) {
System.out.println(counter++ + ". C=" + (c-k) + ", B=" + j + ", A=" + (75-d));
}
k++;
}
}
}
Dumbo change your name.. u r not one :P . Till now it looks good but not very much sure :)
It will be the coefficient of x^100 in (1+x+x^2+.........x^75)*(1+x+x^2+.........x^50)*(1+x+x^2+.........x^25)
Constraints wont be maintained. With this you also will get output as 75 + 50 + 25 and so on and so forth
for (i=1;i<=75;i++)
{
for (j=1;j<=50;j++)
{
for (k=1;k<=25;k++)
{
if((i+j+k)==100) // ADD this condition
{
System.out.println("A=" +i);
System.out.println("B=" +j);
System.out.println("C=" +k);
}
@Nascent
Good one, though, code needs some more fixes:
for (i=25;i<=75;i++) // A can be started from 25, because B+C can together hold a maximum of 75
for (j=0;j<=50;j++) // j should start from 0, because B can be 0
for (k=0;k<=25;k++) // k should start from 0, because C can be 0.
Optimized version. It's producing 1001 combinations.
- techie guy July 10, 2013