My problem is given as CODESPTB ON SPOJ . My program is working correctly on my IDE NetBeans but when I am submitting this program on SPOJ it is not submitting correctly.
It is saying time limit exceeded or sometimes it is saying Runtime Error NZEC.
public class Main {
public static void main(String[] args) throws java.lang.Exception {
java.util.Scanner s=new java.util.Scanner(System.in);
//System.out.println(s.hasNext());
//if(s.hasNext()){
int firstLine=s.nextInt();
int[][] finalArray=new int[firstLine][];
for(int i=0;i<firstLine;i++){
/* if(s.hasNextInt()){*/ int secondLine= s.nextInt();
int[] thirdLineArray=new int[secondLine];
for(int j=0;j<secondLine;j++){
thirdLineArray[j]=s.nextInt();
}
finalArray[i]=thirdLineArray;
}
//}
PassingMultiDimArray(finalArray);
// for(int[] x:finalArray){
// System.out.println();
// for(int y:x){
// System.out.print(y+" ");
// }
// }
//}
}
private static void PassingMultiDimArray(int[][] finalArray) {
for(int i=0;i<finalArray.length;i++){
countSwapInsertSort(finalArray[i]);
}
}
private static void countSwapInsertSort(int[] a) {
// throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
// int a[]={14,44,37,20,43,20,33,55};
int swap=0;
for(int i=1;i<a.length;i++){
for(int j=i;j>0;j--){
if(a[j]<a[j-1]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
swap++;
}
else break;
}
}
//System.out.println(Arrays.toString(a));
System.out.println(swap);
}
}
Please tell my mistake, how to submit this problem on SPOJ corectly?
for(int i=1;i<a.length;i++){
for(int j=i;j>0;j--){
if(a[j]<a[j-1]){
int temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
swap++;
}
else break;
}
High time complexity O(n2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.