Created
May 3, 2011 12:42
-
-
Save stphung/953258 to your computer and use it in GitHub Desktop.
TopCoder SRM 505 Division 2 - 500 point problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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