`{"id":"fb300d0b-a2e7-4899-90f0-15102e857c85","name":"First Move In A Nim Game","description":"Two players Alice and Bob are playing NIM Game with each other. Both are playing optimally.Alice starts the game. The task is to find the number of ways of playing 1st move for Alice to ensure a winning strategy for him if possible, otherwise print -1.","inputFormat":"The first line contains one integer n.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].","outputFormat":"Print the number of possible first move for Alice to win. If Alice can't win, Print -1.","constraints":"1&lt;= n &lt;= 10^5\r\n1&lt;= A[i] &lt;= 10^8","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\n\r\npublic class Main {\r\n\r\n static int Solve(int[] arr) {\r\n\r\n }\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 int n = Integer.parseInt(br.readLine());\r\n int arr[] = new int[n];\r\n String[] st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(st[i]);\r\n }\r\n System.out.println(Solve(arr));\r\n }\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n24 6 10 56 9 1 24\r\n","sampleOutput":"1\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":"e941671b-d9c9-4f0d-9edc-a70d0a68d5b4","name":"First Move In A Nim Game","slug":"first-move-in-a-nim-game","type":1}],"next":{"id":"e4f114f9-46b2-4ae6-a6e8-726cf566c0be","name":"A Modified Game Of Nim","type":1,"slug":"a-modified-game-of-nim"},"prev":{"id":"2d5c3f5e-59c7-4590-9d77-9e54746a5bed","name":"Buddy Nim","type":1,"slug":"buddy-nim"}}}`

# First Move In A Nim Game

Two players Alice and Bob are playing NIM Game with each other. Both are playing optimally.Alice starts the game. The task is to find the number of ways of playing 1st move for Alice to ensure a winning strategy for him if possible, otherwise print -1.

`{"id":"fb300d0b-a2e7-4899-90f0-15102e857c85","name":"First Move In A Nim Game","description":"Two players Alice and Bob are playing NIM Game with each other. Both are playing optimally.Alice starts the game. The task is to find the number of ways of playing 1st move for Alice to ensure a winning strategy for him if possible, otherwise print -1.","inputFormat":"The first line contains one integer n.\r\nThe second line contains n space separated integers a[1],a[2]...a[n].","outputFormat":"Print the number of possible first move for Alice to win. If Alice can't win, Print -1.","constraints":"1&lt;= n &lt;= 10^5\r\n1&lt;= A[i] &lt;= 10^8","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\n\r\npublic class Main {\r\n\r\n static int Solve(int[] arr) {\r\n\r\n }\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 int n = Integer.parseInt(br.readLine());\r\n int arr[] = new int[n];\r\n String[] st = br.readLine().split(\" \");\r\n for (int i = 0; i < n; i++) {\r\n arr[i] = Integer.parseInt(st[i]);\r\n }\r\n System.out.println(Solve(arr));\r\n }\r\n}\r\n"},"ruby":{"code":""},"python":{"code":""},"javascript":{"code":""}},"points":10,"difficulty":"medium","sampleInput":"7\r\n24 6 10 56 9 1 24\r\n","sampleOutput":"1\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":"e941671b-d9c9-4f0d-9edc-a70d0a68d5b4","name":"First Move In A Nim Game","slug":"first-move-in-a-nim-game","type":1}],"next":{"id":"e4f114f9-46b2-4ae6-a6e8-726cf566c0be","name":"A Modified Game Of Nim","type":1,"slug":"a-modified-game-of-nim"},"prev":{"id":"2d5c3f5e-59c7-4590-9d77-9e54746a5bed","name":"Buddy Nim","type":1,"slug":"buddy-nim"}}}`

Editor

# First Move In A Nim Game

medium

Two players Alice and Bob are playing NIM Game with each other. Both are playing optimally.Alice starts the game. The task is to find the number of ways of playing 1st move for Alice to ensure a winning strategy for him if possible, otherwise print -1.

## Constraints

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

## Format

### Input

The first line contains one integer n. The second line contains n space separated integers a[1],a[2]...a[n].

### Output

Print the number of possible first move for Alice to win. If Alice can't win, Print -1.

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

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

Discussions

Show Discussion

Related Resources