Skip to content

Instantly share code, notes, and snippets.

@ozkansari
Last active August 29, 2015 14:02
Show Gist options
  • Save ozkansari/261cd65b0302849a7c50 to your computer and use it in GitHub Desktop.
Save ozkansari/261cd65b0302849a7c50 to your computer and use it in GitHub Desktop.
TopCoder: SRM 625 Div2 Round 1
/**
* TopCoder: SRM 625 Div2 Round 1
*
* http://community.topcoder.com/stat?c=problem_statement&pm=13231&rd=15858&rm=322742&cr=20029384
*
* You are given an int y. We are looking for any int[] x that satisfies the following constraints:
* - No x[i] can be equal to 0 or 1.
* - Each x[i] must be between -1000 and 1000, inclusive.
* - ( x[0] * x[1] ) + x[2] = y
* - x has exactly three elements
*
*/
public class AddMultiply {
/**
* @param y between 0 and 500, inclusive.
* @return int[] x has exactly three elements
*/
public int[] makeExpression(int y) {
for (int x2 = -1000; x2 <=1000; x2++) {
// No x[i] can be equal to 0 or 1.
if(x2==0 || x2==1) {
continue;
}
// ( x[0] * x[1] ) + x[2] = y
int x0x1 = y-x2;
// Each x[i] must be between -1000 and 1000, inclusive.
for (int x0 = -1000; x0 <=1000; x0++) {
if(x0==0 || x0==1) {
continue;
}
// find x1 for gişven x0*x1 and x0
int x1 = x0x1/x0;
// Check if suitable
if(x1*x2==x0x1 && x1!=0 && x1!=1 ) {
int[] x = {x0,x1,x2};
return x;
}
}
}
return null;
}
}
public class AddMultiplyBest {
public int[] makeExpression(int y) {
int[] x = new int[3];
if (y == 6 || y == 7) {
x[0] = 2;
x[1] = 2;
} else {
x[0] = 2;
x[1] = 3;
}
x[2] = y - x[0] * x[1];
return x;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment