Hello,
I am new to programming. I am working on the problem “CMPLS - Complete the Sequence!” and wrote a program in JAVA using recursion. All the test cases are passed correct through my code both on my compiler and here on Ideone. I also tried to check it with SPOJ toolkit and got the code working fine. But I am getting WA when I submit it as a solution here. Could anybody please tell me where I might be making the mistake? Thank you.
Here’s my code -
import java.util.Arrays;
import java.util.ArrayList;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception{
ArrayList<int[]> input = new ArrayList<>();
ArrayList<int[]> inputlen = new ArrayList<>();
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(r);
int t = Integer.parseInt(br.readLine());
while(t > 0) {
int[] serieslenparam = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
int[] seriesgiven = Arrays.stream(br.readLine().split("\\s+")).mapToInt(Integer::parseInt).toArray();
inputlen.add(serieslenparam);
input.add(seriesgiven);
t--;
}
br.close();
r.close();
for(int n = 0; n < inputlen.size(); n++) {
int[] out = polyfill(inputlen.get(n)[0]-1, input.get(n), inputlen.get(n)[1]);
for(int m = 0; m < out.length; m++) {
System.out.print(out[m] + " ");
}
System.out.println();
}
}
public static int[] polyfill(int sn, int[] tempseries, int cn) {
int[] outseries = new int[cn];
if(sn == 0) {
for(int i = 0; i < outseries.length; i++) {
outseries[i] = tempseries[sn];
}
return outseries;
}
int[] newseries = new int[tempseries.length - 1];
int consti = 0;
boolean flag = false;
for(int i = 0; i < sn; i++) {
int sub = tempseries[i+1] - tempseries[i];
if(i == 0) {
consti = sub;
}
newseries[i] = sub;
flag = (newseries[i] == consti) ? true : false;
}
if(flag == false) {
int[] retseries = polyfill(sn - 1, newseries, cn);
outseries[0] = tempseries[sn] + retseries[0];
for(int m = 0; m < cn-1; m++) {
outseries[m+1] = outseries[m] + retseries[m+1];
}
return outseries;
}else {
outseries[0] = tempseries[sn] + consti;
for(int k = 0; k < cn-1; k++) {
outseries[k+1] = outseries[k] + consti;
}
return outseries;
}
}
}
https://ideone.com/PTqBOX