Project Euler #11: Largest product in a grid

  • + 1 comment
    import java.util.*;
    class Solution
    {
        static int[][] mat= new int[20][20];
        static int greatestone=0;
        static void checkVerticalFwd(int x, int y)
        {
            if((x+3)>=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y];
            temp*=mat[x+2][y];
            temp*=mat[x+3][y];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
         static void checkHorizontalFwd(int x, int y)
        {
            if((y+3)>=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x][y+1];
            temp*=mat[x][y+2];
            temp*=mat[x][y+3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
         static void DiagRight(int x, int y)
        {
            if((x+3)>=20|| y + 3 >=20)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y+1];
            temp*=mat[x+2][y+2];
            temp*=mat[x+3][y+3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
        static void DiagLeft(int x, int y)
        {
            if((x+3)>=20||(y - 3) < 0)
            {
                return;
            }
            int temp= mat[x][y];
            temp*=mat[x+1][y-1];
            temp*=mat[x+2][y-2];
            temp*=mat[x+3][y-3];
            if(temp>greatestone)
            {
                greatestone=temp;
            }
        }
        public static void main(String[] args)
        {
            Scanner scn = new Scanner(System.in);
            
             for(int x = 0; x < 20; x++){
                    for(int y = 0; y < 20; y++){
                        mat[x][y] = scn.nextInt();
                    }
                }
            
                for(int x = 0; x < 20; x++) {
                    for(int y = 0; y < 20; y++) {
                        checkVerticalFwd(x, y);
                        checkHorizontalFwd(x,y);
                        DiagRight(x,y);
                        DiagLeft(x,y) ;
                    }
    								System.out.println(greatestone);
    
            }
            System.out.println(greatestone);
    }
    

    }