Run Code and Submit giving different verdicts
This solution is getting accepted.\n\nclass Solution {\npublic:\n    int grid[500][500];\n    int ans[500][500][4];\n    int orderOfLargestPlusSign(int n, vecto
This solution is getting accepted.\n```\nclass Solution {\npublic:\n    int grid[500][500];\n    int ans[500][500][4];\n    int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                grid[i][j]= 1;\n            }\n        }\n        for(auto e: mines)\n        {\n            grid[e[0]][e[1]]= 0;\n        }\n        if(mines.size() == n*n)\n        {\n            return 0;\n        }\n        vector<pair<int, int>> moves= {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                if(grid[i][j])\n                {\n                    for(int ind= 2; ind < 4; ind++)\n                    {\n                        ans[i][j][ind]= 0;\n                        int x1= moves[ind].first;\n                        int y1= moves[ind].second;\n                        if((i+x1 < 0) || (i+x1 >= n) || (j+y1 < 0) || (j+y1 >= n))\n                        {\n                            continue;\n                        }\n                        if(grid[i+x1][j+y1])\n                        {\n                            ans[i][j][ind]= ans[i+x1][j+y1][ind]+1;\n                        }\n                    }\n                }\n            }\n        }\n        for(int i= n-1; i >= 0; i--)\n        {\n            for(int j= n-1; j >= 0; j--)\n            {\n                if(grid[i][j])\n                {\n                    for(int ind= 0; ind < 2; ind++)\n                    {\n                        ans[i][j][ind]= 0;\n                        int x1= moves[ind].first;\n                        int y1= moves[ind].second;\n                        if((i+x1 < 0) || (i+x1 >= n) || (j+y1 < 0) || (j+y1 >= n))\n                        {\n                            continue;\n                        }\n                        if(grid[i+x1][j+y1])\n                        {\n                            ans[i][j][ind]= ans[i+x1][j+y1][ind]+1;\n                        }\n                    }\n                }\n            }\n        }\n        int ret= 0;\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                int temp= 1e9;\n                for(int ind= 0; ind < 4; ind++)\n                {\n                    temp= min(temp, ans[i][j][ind]);\n                }\n                ret= max(ret, temp);\n            }\n        }\n        return (ret+1);\n    }\n};\n```\nBut this is giving wrong answer.\n```\nclass Solution {\npublic:\n    int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {\n        int grid[500][500];\n        int ans[500][500][4];\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                grid[i][j]= 1;\n            }\n        }\n        for(auto e: mines)\n        {\n            grid[e[0]][e[1]]= 0;\n        }\n        if(mines.size() == n*n)\n        {\n            return 0;\n        }\n        vector<pair<int, int>> moves= {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                if(grid[i][j])\n                {\n                    for(int ind= 2; ind < 4; ind++)\n                    {\n                        ans[i][j][ind]= 0;\n                        int x1= moves[ind].first;\n                        int y1= moves[ind].second;\n                        if((i+x1 < 0) || (i+x1 >= n) || (j+y1 < 0) || (j+y1 >= n))\n                        {\n                            continue;\n                        }\n                        if(grid[i+x1][j+y1])\n                        {\n                            ans[i][j][ind]= ans[i+x1][j+y1][ind]+1;\n                        }\n                    }\n                }\n            }\n        }\n        for(int i= n-1; i >= 0; i--)\n        {\n            for(int j= n-1; j >= 0; j--)\n            {\n                if(grid[i][j])\n                {\n                    for(int ind= 0; ind < 2; ind++)\n                    {\n                        ans[i][j][ind]= 0;\n                        int x1= moves[ind].first;\n                        int y1= moves[ind].second;\n                        if((i+x1 < 0) || (i+x1 >= n) || (j+y1 < 0) || (j+y1 >= n))\n                        {\n                            continue;\n                        }\n                        if(grid[i+x1][j+y1])\n                        {\n                            ans[i][j][ind]= ans[i+x1][j+y1][ind]+1;\n                        }\n                    }\n                }\n            }\n        }\n        int ret= 0;\n        for(int i= 0; i < n; i++)\n        {\n            for(int j= 0; j < n; j++)\n            {\n                int temp= 1e9;\n                for(int ind= 0; ind < 4; ind++)\n                {\n                    temp= min(temp, ans[i][j][ind]);\n                }\n                ret= max(ret, temp);\n            }\n        }\n        return (ret+1);\n    }\n};\n```\nThe only difference between the two codes is that the two arrays(grid, ans) have been moved inside the function in the second code.\nI get wrong answer when I submit but running the code for that failing case gives correct answer. This does not have any global values which might not be updated, so I am not able to understand why the first one works but the second one doesn\'t. Can someone please explain what is happening.
[]