On Sunday, 7 April 2019 18:57:19 UTC+5:30, Vinodharanie Sivalingam wrote: > Can anyone tell what are the possible reasons for getting RE in > cryptopanagram in java?
In the question, they have mentioned that factors are odd numbers so prime number "2" is also invalid. Note: "because we worry that it is too easy to factor even numbers." This is my working code in java. import java.math.BigInteger; import java.util.*; public class Crypto { public static void main(String[] args) { List<String> output = new LinkedList<>(); Scanner sc = new Scanner(System.in); int noOfTestCases = sc.nextInt(); for (int testCase = 0; testCase < noOfTestCases; testCase++) { BigInteger n = sc.nextBigInteger(); int lengthOfText = sc.nextInt(); BigInteger[] array = new BigInteger[lengthOfText + 1]; BigInteger[] multipleArray = new BigInteger[lengthOfText]; Set<BigInteger> cloneArray = new TreeSet<>(); int temp = -1; for (int i = 0; i < lengthOfText; i++) { multipleArray[i] = sc.nextBigInteger(); if (i > 0 && !multipleArray[i].equals(multipleArray[i - 1]) && temp == -1) { temp = i - 1; } } BigInteger common = getCommonFactor(multipleArray[temp], multipleArray[temp + 1]); array[temp + 1] = common; cloneArray.add(common); for (int i = temp; i >= 0; i--) { array[i] = multipleArray[i].divide(array[i + 1]); cloneArray.add(array[i]); } for (int i = temp + 1; i < lengthOfText; i++) { array[i+1] = multipleArray[i].divide(array[i]); cloneArray.add(array[i+1]); } StringBuilder text = new StringBuilder(); for (int j = 0; j < lengthOfText + 1; j++) { int count = 0; for (BigInteger bigInteger : cloneArray) {//9 9 9 15 55 121 77 91 39 if (bigInteger.equals(array[j])) { text.append((char) (65 + count)); break; } count++; } } output.add("Case #" + (testCase + 1) + ": " + text); } for (String print : output) { System.out.println(print); } } private static BigInteger getCommonFactor(BigInteger num1, BigInteger num2) { while (!num1.equals(BigInteger.ZERO) && !num2.equals(BigInteger.ZERO)) { if (num1.compareTo(num2) > 0) { num1 = num1.mod(num2); } else { num2 = num2.mod(num1); } } return num1.equals(BigInteger.ZERO) ? num2 : num1; } } -- You received this message because you are subscribed to the Google Groups "Google Code Jam" group. To unsubscribe from this group and stop receiving emails from it, send an email to google-code+unsubscr...@googlegroups.com. To post to this group, send email to google-code@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/google-code/f47b139e-d302-4550-b83a-16e869b750d6%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.