✅ O(1) || [Java / C++] || General logic for 8 directions move || The Main Intuition ||
Intuition\n Describe your first thoughts on how to solve this problem. \n1. Check 8 directions around the King.\n2. Find the nearest queen in each direction.\n\
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n1. Check 8 directions around the King.\n2. Find the nearest queen in each direction.\n\n\n# Complexity\n- Time complexity: $$O(8*8)$$ ~ $$O(1 )$$\nIf it is a general matrix then T.C - $$O(m*n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(8*8)$$ ~ $$O(1)$$\nIf it is a general matrix then for seen array S.C - $$O(m*n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n```\nif(you like)\n\tplease please UPVOTE\uD83D\uDE0A\uD83D\uDE0A;\n```\n\n`Please read the comment in the code carefully then you will undersatnd everything.`\n\n# Java Code\n```\nclass Solution {\n    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {\n        List<List<Integer>> result = new ArrayList<>();\n        boolean[][] seen = new boolean[8][8];\n        for (int[] queen : queens) seen[queen[0]][queen[1]] = true; // Mark seen where the queen is present\n\n        // This nested loop will generate all 8 directions.        \n        for (int dx = -1; dx <=1; dx++) {\n            for (int dy = -1; dy <=1; dy++) {\n                if (dx == 0 && dy == 0) continue; // means the position where the king is present so no need to go inside.\n                int x = king[0], y = king[1];\n                while (x + dx >= 0 && x + dx < 8 && y + dy >= 0 && y + dy < 8) // Check the new position if it is inside the chessboard boundary.\n                {\n                    x += dx;\n                    y += dy;\n                    if (seen[x][y]) // We reach one of the queens position so this quuen will attack\n                    {   \n                        result.add(Arrays.asList(x, y));\n                        break; // This direction is complete try other direction.\n                    }\n                }\n            }\n        }\n        return result;\n    }\n}\n```\n# C++ Code\n```\nclass Solution {\npublic:\n    vector<vector<int>> queensAttacktheKing(vector<vector<int>>& queens, vector<int>& king) {\n        vector<vector<int>> result;\n        bool seen[8][8];\n        memset(seen, false, sizeof(seen));\n        for (vector<int> queen : queens) seen[queen[0]][queen[1]] = true; // Mark the positions seen where the queens are present\n\n        // This nested loop will generate all 8 directions.        \n        for (int dx = -1; dx <=1; dx++) \n        {\n            for (int dy = -1; dy <=1; dy++) \n            {\n                if (dx == 0 && dy == 0) continue; // means the position where the king is present so no need to inside.\n\n                int x = king[0], y = king[1];\n                while (x + dx >= 0 && x + dx < 8 && y + dy >= 0 && y + dy < 8) // Check the new position if it is inside the chessboard boundary.\n                {\n                    x += dx;  // New position of x in that particular direction\n                    y += dy;  // New position of y in that particular direction\n                    if (seen[x][y]) // We reach one of the queens position in that particular direction so this queen will attack the king\n                    {   \n                        vector<int> temp;\n                        temp.push_back(x);\n                        temp.push_back(y);\n                        result.push_back(temp);\n                        temp.clear();\n                        break; // This direction is complete try other direction.\n                    }\n                }\n            }\n        }\n        return result;\n    }\n};\n```\n\n
['Array', 'Matrix', 'Simulation', 'C++', 'Java']