`{"id":"bbfb3bd7-2adc-4058-9f6a-778ab2fa492b","name":"Buddy Nim","description":"Three players Alice and Bob and Charlie are playing a game. There are two tables, on the first table, there are N heaps containing A[1],A[2].... A[n] coins, on the second table , there are M heaps containing B[1], B[2]... B[m] coins.\r\nInitially, Alice is playing at the first table and Bob is playing at the second table. The players take their turns in this order: Charlie, Alice, Bob, Charlie, etc.\r\nBob and alice in their turn can remove as much coin as they want(min is 1) from any one pile of their respective tables. Whoever cannot remove any stone from a pile loses.\r\nCharlie does not play at any table. Instead, on his turn, he decides if Alice and Bob should keep playing at their respective tables or swap places.\r\nAlice and Charlie are buddies and they cooperate, playing in the optimal way that results in Alice's victory (if possible). Find out whether alice will win or bob.\r\n","inputFormat":"The first line contains two space separated integers n and m.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].\r\nThe third line contains m space separated integers b[1], b[2]....b[n].","outputFormat":"Print the winner (ALICE or BOB).","constraints":"1&lt;= n,m &lt;= 10^5\r\n1&lt;= A[i] &lt;= 10^8\r\n1&lt;= B[i] &lt;= 10^8","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.util.Arrays;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws NumberFormatException, IOException {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int t = 1;\r\n StringBuilder sb = new StringBuilder();\r\n while (t-- > 0) {\r\n String[] st = br.readLine().split(\" \");\r\n int n = Integer.parseInt(st[0]);\r\n int m = Integer.parseInt(st[1]);\r\n\r\n int[] arr1 = new int[n];\r\n int[] arr2 = new int[m];\r\n\r\n st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr1[i] = Integer.parseInt(st[i]);\r\n }\r\n st = br.readLine().split(\" \");\r\n for (int i = 0; i < m; i++) {\r\n arr2[i] = Integer.parseInt(st[i]);\r\n }\r\n }\r\n\r\n }\r\n\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6 7\r\n24 6 10 56 9 1\r\n0 6 24 1 9 56 10","sampleOutput":"BOB\r\n\r\n","questionVideo":"","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":"1191e2be-22c8-444b-988f-201dc78b143e","name":"Game Theory For Experts","slug":"game-theory-for-experts-930","type":0},{"id":"2d5c3f5e-59c7-4590-9d77-9e54746a5bed","name":"Buddy Nim","slug":"buddy-nim","type":1}],"next":{"id":"e941671b-d9c9-4f0d-9edc-a70d0a68d5b4","name":"First Move In A Nim Game","type":1,"slug":"first-move-in-a-nim-game"},"prev":{"id":"e15d91ef-6e16-48d5-ab94-31a70c26b00a","name":"Nim Game","type":1,"slug":"nim-game"}}}`

# Buddy Nim

Three players Alice and Bob and Charlie are playing a game. There are two tables, on the first table, there are N heaps containing A[1],A[2].... A[n] coins, on the second table , there are M heaps containing B[1], B[2]... B[m] coins. Initially, Alice is playing at the first table and Bob is playing at the second table. The players take their turns in this order: Charlie, Alice, Bob, Charlie, etc. Bob and alice in their turn can remove as much coin as they want(min is 1) from any one pile of their respective tables. Whoever cannot remove any stone from a pile loses. Charlie does not play at any table. Instead, on his turn, he decides if Alice and Bob should keep playing at their respective tables or swap places. Alice and Charlie are buddies and they cooperate, playing in the optimal way that results in Alice's victory (if possible). Find out whether alice will win or bob.

`{"id":"bbfb3bd7-2adc-4058-9f6a-778ab2fa492b","name":"Buddy Nim","description":"Three players Alice and Bob and Charlie are playing a game. There are two tables, on the first table, there are N heaps containing A[1],A[2].... A[n] coins, on the second table , there are M heaps containing B[1], B[2]... B[m] coins.\r\nInitially, Alice is playing at the first table and Bob is playing at the second table. The players take their turns in this order: Charlie, Alice, Bob, Charlie, etc.\r\nBob and alice in their turn can remove as much coin as they want(min is 1) from any one pile of their respective tables. Whoever cannot remove any stone from a pile loses.\r\nCharlie does not play at any table. Instead, on his turn, he decides if Alice and Bob should keep playing at their respective tables or swap places.\r\nAlice and Charlie are buddies and they cooperate, playing in the optimal way that results in Alice's victory (if possible). Find out whether alice will win or bob.\r\n","inputFormat":"The first line contains two space separated integers n and m.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].\r\nThe third line contains m space separated integers b[1], b[2]....b[n].","outputFormat":"Print the winner (ALICE or BOB).","constraints":"1&lt;= n,m &lt;= 10^5\r\n1&lt;= A[i] &lt;= 10^8\r\n1&lt;= B[i] &lt;= 10^8","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.util.Arrays;\r\n\r\npublic class Main {\r\n\r\n public static void main(String[] args) throws NumberFormatException, IOException {\r\n BufferedReader br = new BufferedReader(new InputStreamReader(System.in));\r\n\r\n int t = 1;\r\n StringBuilder sb = new StringBuilder();\r\n while (t-- > 0) {\r\n String[] st = br.readLine().split(\" \");\r\n int n = Integer.parseInt(st[0]);\r\n int m = Integer.parseInt(st[1]);\r\n\r\n int[] arr1 = new int[n];\r\n int[] arr2 = new int[m];\r\n\r\n st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr1[i] = Integer.parseInt(st[i]);\r\n }\r\n st = br.readLine().split(\" \");\r\n for (int i = 0; i < m; i++) {\r\n arr2[i] = Integer.parseInt(st[i]);\r\n }\r\n }\r\n\r\n }\r\n\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"6 7\r\n24 6 10 56 9 1\r\n0 6 24 1 9 56 10","sampleOutput":"BOB\r\n\r\n","questionVideo":"","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":"1191e2be-22c8-444b-988f-201dc78b143e","name":"Game Theory For Experts","slug":"game-theory-for-experts-930","type":0},{"id":"2d5c3f5e-59c7-4590-9d77-9e54746a5bed","name":"Buddy Nim","slug":"buddy-nim","type":1}],"next":{"id":"e941671b-d9c9-4f0d-9edc-a70d0a68d5b4","name":"First Move In A Nim Game","type":1,"slug":"first-move-in-a-nim-game"},"prev":{"id":"e15d91ef-6e16-48d5-ab94-31a70c26b00a","name":"Nim Game","type":1,"slug":"nim-game"}}}`

Editor

# Buddy Nim

medium

Three players Alice and Bob and Charlie are playing a game. There are two tables, on the first table, there are N heaps containing A[1],A[2].... A[n] coins, on the second table , there are M heaps containing B[1], B[2]... B[m] coins. Initially, Alice is playing at the first table and Bob is playing at the second table. The players take their turns in this order: Charlie, Alice, Bob, Charlie, etc. Bob and alice in their turn can remove as much coin as they want(min is 1) from any one pile of their respective tables. Whoever cannot remove any stone from a pile loses. Charlie does not play at any table. Instead, on his turn, he decides if Alice and Bob should keep playing at their respective tables or swap places. Alice and Charlie are buddies and they cooperate, playing in the optimal way that results in Alice's victory (if possible). Find out whether alice will win or bob.

## Constraints

1<= n,m <= 10^5 1<= A[i] <= 10^8 1<= B[i] <= 10^8

## Format

### Input

The first line contains two space separated integers n and m. The second line contains n space separated integers a[1],a[2]...a[n]. The third line contains m space separated integers b[1], b[2]....b[n].

### Output

Print the winner (ALICE or BOB).

## 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;}6 7 24 6 10 56 9 1 0 6 24 1 9 56 10```

### 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;}BOB ```

Discussions

Show Discussion

Related Resources