I am not able to find the error in my code thats causing run time error. Code-
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
// TODO Auto-generated method stub
int t;
Scanner sc = new Scanner(new BufferedReader(new
InputStreamReader(System.in)));
t = sc.nextInt();
char[] alpha = new char[30];
alpha[0] = 'A';
alpha[1] = 'B';
alpha[2] = 'C';
alpha[3] = 'D';
alpha[4] = 'E';
alpha[5] = 'F';
alpha[6] = 'G';
alpha[7] = 'H';
alpha[8] = 'I';
alpha[9] = 'J';
alpha[10] = 'K';
alpha[11] = 'L';
alpha[12] = 'M';
alpha[13] = 'N';
alpha[14] = 'O';
alpha[15] = 'P';
alpha[16] = 'Q';
alpha[17] = 'R';
alpha[18] = 'S';
alpha[19] = 'T';
alpha[20] = 'U';
alpha[21] = 'V';
alpha[22] = 'W';
alpha[23] = 'X';
alpha[24] = 'Y';
alpha[25] = 'Z';
for(int v=1; v<=t; v++)
{
int n,l;
n=sc.nextInt();
l=sc.nextInt();
int[] cipher = new int[l+1];
for(int j=1; j<=l; j++)
{
cipher[j] = sc.nextInt();
}
int[] firstfour = new int[5];
int itr = 0;
int fprime;
for(int k=1; k<=2; k++)
{
boolean []isPrime= new boolean[cipher[k] + 1];
SieveOfEratosthenes(cipher[k], isPrime);
// Traversing all numbers to find first
// pair
for (int i = 2; i < cipher[k]; i++) {
int x = cipher[k] / i;
if (isPrime[i] && isPrime[x] && x != i && x * i ==
cipher[k])
{
firstfour[itr++] = i;
firstfour[itr++] = x;
break;
}
}
}
int flag = 0;
for(int y=2; y<4; y++)
{
if(firstfour[1] == firstfour[y])
{
flag = 1;
}
}
if(flag == 1)
{
fprime = firstfour[0];
}
else
{
fprime = firstfour[1];
}
int[] primes = new int[l+2];
int[] asprimes = new int[l+1];
int[] sortprimes = new int[26];
primes[1] = fprime;
asprimes[0] = fprime;
int scount = 0;
for(int h=1; h<=l; h++)
{
fprime = cipher[h]/fprime;
primes[h+1] = fprime;
asprimes[h] = fprime;
}
Arrays.sort(asprimes);
sortprimes[0] = asprimes[0];
for(int h=0; h<l; h++)
{
if(asprimes[h] != asprimes[h+1])
{
scount++;
sortprimes[scount] = asprimes[h+1];
}
}
String out = "";
for(int a=1; a<=l+1; a++)
{
for(int b=0; b<26; b++)
{
if(primes[a] == sortprimes[b])
{
out = out + alpha[b];
}
}
}
System.out.println("Case #"+v+": "+out);
}
}
static void SieveOfEratosthenes(int n, boolean isPrime[])
{
isPrime[0] = isPrime[1] = false;
for (int i = 2; i <= n; i++)
isPrime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (isPrime[p] == true) {
for (int i = p * 2; i <= n; i += p)
isPrime[i] = false;
}
}
}
}
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/google-code/e9b1b599-f009-4eb8-bb24-06cd462e2f08%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
