I have tried question so many test cases on my own too and I’m getting right answers but whenever I submit it is giving me Wrong Answer. Please Help me with my code. I am using bruteforce approch here.
Please suggest any test case that would help solve my problem.
Here is my code with proper comments so please have a look at it.
/*
Input are taken in these two arrays respectively
int[] start=new int[n];
int[] end=new int[n];
*/
int[][] j=new int[n][2]; //arrays to store the start and end time at j[i][0] ans j[i][1] respectively for the timimgs of activity on which J will be busy
int[][] c=new int[n][2]; // similar array for c
for(int i=0;i<n;i++) {
for(int k=0;k<2;k++) { //initialising array to -1 because 0 initialization would lead to wrong overlapping
j[i][k]=-1;
c[i][k]=-1;
}
}
int f=1, zj=0, zc=0; // f is flag variable ans zj and zc are used to iterate over j and c arrays.
String ans="", im="IMPOSSIBLE";
for(int i=0;i<n;i++) {
for(int k=0;k<n;k++) {
if(isOverlapping(start[i], end[i], j[k][0], j[k][1])) {
f=0; // if current timing is overlapping with any previous of J than J will not be added.
break;
}else f=1;
}
if(f==0) {
for(int k=0;k<n;k++) {
if(!isOverlapping(start[i], end[i], c[k][0], c[k][1])) {
f=2;
}else {
f=0; // If Ccurrent timing of C also overlaps with any other previous timings of c that it is IMPOSSIBLE
break;
}
}
}
if(f==1) { // If J is free
j[zj][0]=start[i];
j[zj][1]=end[i];
zj++;
ans+='J';
}else if(f==2) { // If C is free
c[zc][0]=start[i];
c[zc][1]=end[i];
zc++;
ans+='C';
f=1;
}else { //If both are busy
ans=im;
break;
}
}