I have written code for indicium. I think it should work but still it is 
failing at google codejam. 
Please help and code is below:


Indicium TC failing while it is passes locally

Inbox

Neeraj Kumar

to codejam
2 days ago

Details

I have tried all possible TCs at local for Indicum problem.Please provide TC 
set for which below code is failing. It will help improvement in coding 
perspective.

Code:

import java.util.Scanner;

public class Solution {
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int T=sc.nextInt();
        int x=1;
        while(T>0){
            int n=sc.nextInt();
            int k=sc.nextInt();
            if(k>n*n || k<n){
                System.out.println("Case #" + x + ": " + "IMPOSSIBLE");
            }
            else {
                int[][] latinSquare = new int[n + 1][n + 1];
                for (int i = 1; i < n + 1; i++) {
                    int p = i;
                    for (int j = 1; j < n + 1; j++) {
                        if (p == n + 1) {
                            p = 1;
                        }
                        latinSquare[i][j] = p++;
                        //System.out.print(latinSquare[i][j]);
                    }
                    //System.out.println();
                }
                boolean visited[] = new boolean[n + 1];
                int[] list=new int[n+1];
                int sum = k;
                boolean flag = getLatinSquareForGivenTrace(latinSquare, 
visited, list, k, n, 0);
                if (flag) {
                    System.out.println("Case #" + x + ": " + "POSSIBLE");
                    for (int i=1;i<=n;i++) {
                        for (int j = 1; j <= n; j++) {
                            if(j==n){
                                System.out.println(latinSquare[list[i]][j]);
                            }
                            else{
                                System.out.print(latinSquare[list[i]][j] + " ");
                            }
                        }
                    }
                } else {
                    System.out.println("Case #" + x + ": " + "IMPOSSIBLE");
                }
            }
            T--;
            x++;
        }
    }

    public static boolean getLatinSquareForGivenTrace(int[][] 
latinSquare,boolean[] visited,int[] list,int sum,int n,int times){
        if(times==n && sum!=0){
            return false;
        }
        if(sum==0 && times==n){
            return true;
        }
        if(sum==0 && times!=n){
            return false;
        }
        times++;
        for(int i=1;i<=n;i++){
            boolean flag=false;
            if(!visited[i]){
                visited[i]=true;
                sum-=latinSquare[i][times];
                Integer num=i;
                list[times]=i;
                
flag=getLatinSquareForGivenTrace(latinSquare,visited,list,sum,n,times);
                if(flag){
                    return flag;
                }
                sum+=latinSquare[i][times];
                visited[i]=false;
            }


        }
        times--;
        return 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 google-code+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/2a7767d1-1895-4019-afd1-14d021a68c5d%40googlegroups.com.

Reply via email to