`{"id":"b71d558f-7247-4c3f-8601-b06f40f16afb","name":"Magnets","description":"1. You are given n number of domino shaped bipolar magnets.\r\n2. You have to place these magnets in M*N following the conditions.\r\n3. Conditions are -\r\n a. Each box of 1*2 or 2*1 can contain a magnet or can be empty.\r\n b. Empty box can be represented by X's and magnets are represented by + and \r\n - sign.\r\n c. Digits along left and top side of the board represents the number of + in \r\n corresponding rows and columns.\r\n d. Digits along right and bottom of the board represents the number of - in \r\n corresponding rows and columns.\r\n e. -1 denotes that the corresponding row and column can have any number of \r\n + and - signs.\r\n f. No two adjacent cell can have the same sign.\r\n\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"A number M\r\nA number N\r\nM*N characters containing only 'L', 'R', 'T' and 'B'.\r\n(For 1*2 box 'L' represents left end and 'R' represents the right end)\r\n(For 2*1 box 'T' represents top end and 'B' represents the right end)\r\nN integers representing count of '+' along the top edge.\r\nM integers representing count of '+' along the left edge.\r\nM integers representing count of '-' along the right edge.\r\nN integers representing count of '-' along the bottom edge.","outputFormat":"Check the sample output and question video","constraints":"2 &lt;= M &lt;= 10\r\n2 &lt;= N &lt;= 10\r\nBoth M and N are not odd.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static boolean solution(char[][] arr, int[] top, int[] left, int[] right, int[] bottom, char[][] ans,\r\n\t\t\tint row, int col) {\r\n\r\n\t\t\t\t//write your code here\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\tpublic static void print(char[][] arr) {\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tfor (int j = 0; j < arr[0].length; j++) {\r\n\t\t\t\tSystem.out.print(arr[i][j] + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint m = scn.nextInt();\r\n\t\tint n = scn.nextInt();\r\n\t\tchar[][] arr = new char[m][n];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tString str = scn.next();\r\n\t\t\tarr[i] = str.toCharArray();\r\n\t\t}\r\n\t\tint[] top = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\ttop[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] left = new int[m];\r\n\t\tfor (int i = 0; i < m; i++) {\r\n\t\t\tleft[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] right = new int[m];\r\n\t\tfor (int i = 0; i < m; i++) {\r\n\t\t\tright[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] bottom = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\tbottom[i] = scn.nextInt();\r\n\t\t}\r\n\r\n\t\t//write your code here\r\n\t}\r\n\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5 6\r\nLRLRTT\r\nLRLRBB\r\nTTTTLR\r\nBBBBTT\r\nLRLRBB\r\n1 -1 -1 2 1 -1\r\n2 3 -1 -1 -1\r\n-1 -1 -1 1 -1\r\n2 -1 -1 2 -1 3","sampleOutput":"+ - + - X - \r\n- + - + X + \r\nX X + - + - \r\nX X - + X + \r\n- + X X X - \r\n","questionVideo":"https://www.youtube.com/embed/vUq70CeNKT4","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"f437aa67-3e6c-4051-bc7d-3851936787a0","name":"Magnets","slug":"magnets","type":1}],"next":{"id":"dbdc0353-e495-4dd2-8278-7cb1de56a8f5","name":"Magnets MCQ","type":0,"slug":"magnets-mcq"},"prev":{"id":"80ee7f83-1e20-4427-b78e-529e0ac4cc89","name":"Words - K Length Words - 4 MCQ","type":0,"slug":"words-k-length-words-4-mcq"}}}`

# Magnets

1. You are given n number of domino shaped bipolar magnets. 2. You have to place these magnets in M*N following the conditions. 3. Conditions are - a. Each box of 1*2 or 2*1 can contain a magnet or can be empty. b. Empty box can be represented by X's and magnets are represented by + and - sign. c. Digits along left and top side of the board represents the number of + in corresponding rows and columns. d. Digits along right and bottom of the board represents the number of - in corresponding rows and columns. e. -1 denotes that the corresponding row and column can have any number of + and - signs. f. No two adjacent cell can have the same sign. Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

`{"id":"b71d558f-7247-4c3f-8601-b06f40f16afb","name":"Magnets","description":"1. You are given n number of domino shaped bipolar magnets.\r\n2. You have to place these magnets in M*N following the conditions.\r\n3. Conditions are -\r\n a. Each box of 1*2 or 2*1 can contain a magnet or can be empty.\r\n b. Empty box can be represented by X's and magnets are represented by + and \r\n - sign.\r\n c. Digits along left and top side of the board represents the number of + in \r\n corresponding rows and columns.\r\n d. Digits along right and bottom of the board represents the number of - in \r\n corresponding rows and columns.\r\n e. -1 denotes that the corresponding row and column can have any number of \r\n + and - signs.\r\n f. No two adjacent cell can have the same sign.\r\n\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"A number M\r\nA number N\r\nM*N characters containing only 'L', 'R', 'T' and 'B'.\r\n(For 1*2 box 'L' represents left end and 'R' represents the right end)\r\n(For 2*1 box 'T' represents top end and 'B' represents the right end)\r\nN integers representing count of '+' along the top edge.\r\nM integers representing count of '+' along the left edge.\r\nM integers representing count of '-' along the right edge.\r\nN integers representing count of '-' along the bottom edge.","outputFormat":"Check the sample output and question video","constraints":"2 &lt;= M &lt;= 10\r\n2 &lt;= N &lt;= 10\r\nBoth M and N are not odd.","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n\tpublic static boolean solution(char[][] arr, int[] top, int[] left, int[] right, int[] bottom, char[][] ans,\r\n\t\t\tint row, int col) {\r\n\r\n\t\t\t\t//write your code here\r\n\r\n\t\treturn false;\r\n\t}\r\n\r\n\tpublic static void print(char[][] arr) {\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tfor (int j = 0; j < arr[0].length; j++) {\r\n\t\t\t\tSystem.out.print(arr[i][j] + \" \");\r\n\t\t\t}\r\n\t\t\tSystem.out.println();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\tScanner scn = new Scanner(System.in);\r\n\t\tint m = scn.nextInt();\r\n\t\tint n = scn.nextInt();\r\n\t\tchar[][] arr = new char[m][n];\r\n\t\tfor (int i = 0; i < arr.length; i++) {\r\n\t\t\tString str = scn.next();\r\n\t\t\tarr[i] = str.toCharArray();\r\n\t\t}\r\n\t\tint[] top = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\ttop[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] left = new int[m];\r\n\t\tfor (int i = 0; i < m; i++) {\r\n\t\t\tleft[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] right = new int[m];\r\n\t\tfor (int i = 0; i < m; i++) {\r\n\t\t\tright[i] = scn.nextInt();\r\n\t\t}\r\n\t\tint[] bottom = new int[n];\r\n\t\tfor (int i = 0; i < n; i++) {\r\n\t\t\tbottom[i] = scn.nextInt();\r\n\t\t}\r\n\r\n\t\t//write your code here\r\n\t}\r\n\r\n\t\r\n}"},"python":{"code":""}},"points":10,"difficulty":"hard","sampleInput":"5 6\r\nLRLRTT\r\nLRLRBB\r\nTTTTLR\r\nBBBBTT\r\nLRLRBB\r\n1 -1 -1 2 1 -1\r\n2 3 -1 -1 -1\r\n-1 -1 -1 1 -1\r\n2 -1 -1 2 -1 3","sampleOutput":"+ - + - X - \r\n- + - + X + \r\nX X + - + - \r\nX X - + X + \r\n- + X X X - \r\n","questionVideo":"https://www.youtube.com/embed/vUq70CeNKT4","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"f437aa67-3e6c-4051-bc7d-3851936787a0","name":"Magnets","slug":"magnets","type":1}],"next":{"id":"dbdc0353-e495-4dd2-8278-7cb1de56a8f5","name":"Magnets MCQ","type":0,"slug":"magnets-mcq"},"prev":{"id":"80ee7f83-1e20-4427-b78e-529e0ac4cc89","name":"Words - K Length Words - 4 MCQ","type":0,"slug":"words-k-length-words-4-mcq"}}}`

Editor

# Magnets

hard

1. You are given n number of domino shaped bipolar magnets. 2. You have to place these magnets in M*N following the conditions. 3. Conditions are - a. Each box of 1*2 or 2*1 can contain a magnet or can be empty. b. Empty box can be represented by X's and magnets are represented by + and - sign. c. Digits along left and top side of the board represents the number of + in corresponding rows and columns. d. Digits along right and bottom of the board represents the number of - in corresponding rows and columns. e. -1 denotes that the corresponding row and column can have any number of + and - signs. f. No two adjacent cell can have the same sign. Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

## Constraints

2 <= M <= 10 2 <= N <= 10 Both M and N are not odd.

## Format

### Input

A number M A number N M*N characters containing only 'L', 'R', 'T' and 'B'. (For 1*2 box 'L' represents left end and 'R' represents the right end) (For 2*1 box 'T' represents top end and 'B' represents the right end) N integers representing count of '+' along the top edge. M integers representing count of '+' along the left edge. M integers representing count of '-' along the right edge. N integers representing count of '-' along the bottom edge.

### Output

Check the sample output and question video

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}5 6 LRLRTT LRLRBB TTTTLR BBBBTT LRLRBB 1 -1 -1 2 1 -1 2 3 -1 -1 -1 -1 -1 -1 1 -1 2 -1 -1 2 -1 3```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}+ - + - X - - + - + X + X X + - + - X X - + X + - + X X X - ```

Question Video

Discussions

Show Discussion

Related Resources