IMC Trading OA

2399
Sep 07, 2022

Coding Roound:

  1. Does a path exist?
  • Easy backracking with memoisation

image
image

My Code:

public class Main {
    static int[][] cache;
    public static void main(String[] args) {
       // System.out.println("Hello World!");
        cache=new int[1001][1001];
        for(int[] arr : cache){
            Arrays.fill(arr,-1);
        }
        System.out.println(canReach(5, 10, 1, 14, 1));
        System.out.println(canReach(5, 1, 10, 1, 14));
        System.out.println(canReach(1, 20, 20, 24, 24));
        System.out.println(canReach(5, 10, 12, 340, 352));
        
    }
static String canReach(int c,int x1,int y1,int x2,int y2){
        if(dfs(c, x1, y1, x2, y2)==1)
            return "Yes";

        return "No";
    }
    
    static int  dfs(int c,int x1,int y1,int x2,int y2){
        if(cache[x1][y1]!=-1)
            return cache[x1][y1];

        if (Math.sqrt(x1 + y1)%1==0)
            return 0;
        if (x1 > x2 || y1 > y2)
            return 0;
        if(x1 == x2 && y1 == y2)
            return 1;

        int a=dfs(c, x1 + c, y1 + c, x2, y2);
        int b=dfs(c, x1 + y1, y1, x2, y2);
        int d=dfs(c, x1, y1 + x1, x2, y2);
        if(a==1||b==1||d==1)
            cache[x1][y1]=1;
        else
            cache[x1][y1]=0;
        return cache[x1][y1];
    }
}
  1. Busy Intersection
  • Not able to pass all test cases 9/13

image
image

My Code - Got it from a friend

image
image
image
image
image
image

Comments (2)