Skip to content

Instantly share code, notes, and snippets.

@stphung
Created May 3, 2011 12:42
Show Gist options
  • Select an option

  • Save stphung/953258 to your computer and use it in GitHub Desktop.

Select an option

Save stphung/953258 to your computer and use it in GitHub Desktop.
TopCoder SRM 505 Division 2 - 500 point problem
public class PerfectSequences {
public String fixIt(int[] seq) {
if (seq.length == 1) return "Yes";
else {
for(int i=0; i<seq.length; i++) {
int sum = sum(seq,i);
int product = product(seq,i);
if (product == 0) continue;
else {
int value = sum/(product-1);
if (sum + value == product * value && value != seq[i]) return "Yes";
}
}
return "No";
}
}
private int sum(int[] seq, int exclude) {
int sum = 0;
for(int i=0; i<seq.length; i++) {
if (exclude != i) sum += seq[i];
}
return sum;
}
private int product(int[] seq, int exclude) {
int product = 1;
for(int i=0; i<seq.length; i++) {
if (exclude != i) product *= seq[i];
}
return product;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment