Given a N x M matrix which represents the number of rows and number of columns respectively. Each cell of a matrix is occupied by exactly one student. The task is to determine whether we can shuffle each student in such a way that each student should occupy a cell that is adjacent to that student’s original cell, i.e. immediately to the left, right, top or bottom of that cell and after shuffling each cell should occupy by exactly one student.

**Examples:**

Input:N = 3, M = 3Output:Shuffling not possible

Input:N = 4, M = 4Output:Shuffing is possibleOne possible way to shuffle the student is as shown below:

**Approach:** Check if the number of rows or the number of columns is even then shuffling is possible otherwise no shuffling is possible.

Below is the implementation of the above approach:

`// C++ implementation of above approach ` `#include <iostream> ` `using` `namespace` `std; `
` ` `// Function that will check whether ` `// Shuffling is possible or not ` `void` `Shuffling(` `int` `N, ` `int` `M) `
`{ ` ` ` `if` `(N % 2 == 0 || M % 2 == 0) `
` ` `cout << ` `"Shuffling is possible"` `; `
` ` `else`
` ` `cout << ` `"Shuffling not possible"` `; `
`} ` ` ` `// Driver code ` `int` `main() `
`{ ` ` ` `int` `N = 4, M = 5; `
` ` ` ` `// Calling function. `
` ` `Shuffling(N, M); `
` ` `return` `0; `
`} ` |

*chevron_right*

*filter_none*

`// Java implementation of above approach ` `import` `java.io.*; `
` ` `class` `GFG `
`{ ` ` ` `// Function that will check whether ` `// Shuffling is possible or not ` `static` `void` `Shuffling(` `int` `N, ` `int` `M) `
`{ ` ` ` `if` `(N % ` `2` `== ` `0` `|| M % ` `2` `== ` `0` `) `
` ` `System.out.println(` `"Shuffling is possible"` `); `
` ` `else`
` ` `System.out.println(` `"Shuffling not possible"` `); `
`} ` ` ` ` ` `// Driver code `
` ` `public` `static` `void` `main (String[] args) `
` ` `{ `
` ` `int` `N = ` `4` `, M = ` `5` `; `
` ` ` ` `// Calling function. `
` ` `Shuffling(N, M); `
` ` `} `
`} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

`# Python3 implementation of above approach ` ` ` `# Function that will check whether ` `# Shuffling is possible or not ` `def` `Shuffling(N, M) : `
` ` ` ` `if` `(N ` `%` `2` `=` `=` `0` `or` `M ` `%` `2` `=` `=` `0` `) : `
` ` `print` `(` `"Shuffling is possible"` `); `
` ` `else` `: `
` ` `print` `(` `"Shuffling not possible"` `); `
` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: `
` ` ` ` `# Driver code `
` ` `N ` `=` `4` `; `
` ` `M ` `=` `5` `; `
` ` ` ` `# Calling function. `
` ` `Shuffling(N, M); `
` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

`// C# implementation of above approach ` `using` `System; `
` ` `class` `GFG `
`{ ` ` ` `// Function that will check whether ` `// Shuffling is possible or not ` `static` `void` `Shuffling(` `int` `N, ` `int` `M) `
`{ ` ` ` `if` `(N % 2 == 0 || M % 2 == 0) `
` ` `Console.Write(` `"Shuffling is possible"` `); `
` ` `else`
` ` `Console.Write(` `"Shuffling not possible"` `); `
`} ` ` ` ` ` `// Driver code `
` ` `public` `static` `void` `Main () `
` ` `{ `
` ` `int` `N = 4, M = 5; `
` ` ` ` `// Calling function. `
` ` `Shuffling(N, M); `
` ` `} `
`} ` ` ` `// This code is contributed by Ita_c. ` |

*chevron_right*

*filter_none*

`<?php ` `// PHP implementation of above approach ` ` ` `// Function that will check whether ` `// Shuffling is possible or not ` `function` `Shuffling(` `$N` `, ` `$M` `) `
`{ ` ` ` `if` `(` `$N` `% 2 == 0 || ` `$M` `% 2 == 0) `
` ` `echo` `"Shuffling is possible"` `; `
` ` `else`
` ` `echo` `"Shuffling not possible"` `; `
`} ` ` ` `// Driver code ` `$N` `= 4; `
`$M` `= 5; `
` ` `// Calling function. ` `Shuffling(` `$N` `, ` `$M` `); `
` ` `// This code is contributed by ` `// Shivi_Aggarwal ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Shuffling is possible

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find cells in Matrix that are not visited by Robot for given movements
- Check if it is possible to make the given matrix increasing matrix or not
- Shuffle an Array using STL in C++
- Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
- Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Maximum trace possible for any sub-matrix of the given matrix
- Remove first adjacent pairs of similar characters until possible
- Check if possible to cross the matrix with given power
- Check for possible path in 2D matrix
- Check if it is possible to create a matrix such that every row has A 1s and every column has B 1s
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Construct a Matrix with no element exceeding X and sum of two adjacent elements not exceeding Y
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Divide Matrix into K groups of adjacent cells having minimum difference between maximum and minimum sized groups
- Maximum product of 4 adjacent elements in matrix
- Replace specified matrix elements such that no two adjacent elements are equal
- Modify matrix by increments such that no pair of adjacent elements are equal
- Program to check diagonal matrix and scalar matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.