# Find the person who will finish last

Last Updated : 18 Jun, 2021

Given a binary matrix mat[][] of dimensions M x N and two-person P1, P2, the task is to find the person who finishes last in choosing a 0 from the matrix which changes to 1 only if the row or column of the cell consisting of 0 has one or more than one 1 in the matrix.

Note: P1 starts picking the 0s first and both the persons want to finish last. The given matrix will always have at least one 0 which could be chosen.

Examples:

Input: mat[][] = {{1, 0, 0}, {0, 0, 0}, {0, 0, 1}}
Output: P1
Explanation
P1 chooses mat[1][1], then the matrix becomes {{1, 0, 0}, {0, 1, 0}, {0, 0,1}}.
P2 has no 0 left to choose from. So, P1 finishes last.

Input: mat[][] = {{0, 0}, {0, 0}}
Output: P2
Explanation
No matter P1 chooses which 0 P2 will always have a 0 to choose and
after P2 picks a 0 there will not be any other 0 to choose from.

Approach: The idea is based on the observation that a 0 can’t be taken if either of its row or column has 1. Follow the steps below to solve this problem:

• Initialize two sets, rows & cols to count the number of rows and columns which does not contain any 1.
• Traverse the matrix and add rows and columns having 1 in it in the set.
• Take the minimum number of rows or columns as if either of them becomes zero so that no more 0s can be taken.
• After finding the minimum number of rows and columns available, if the number of choices made is odd then P1 finishes last otherwise, P2 finishes last.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach #include  using namespace std;   // Function to find the person // who will finish last void findLast(int mat[][3]) {       int m = 3;     int n = 3;       // To keep track of rows     // and columns having 1     set<int> rows;     set<int> cols;       for (int i = 0; i < m; i++) {         for (int j = 0; j < n; j++) {             if (mat[i][j]) {                 rows.insert(i);                 cols.insert(j);             }         }     }       // Available rows and columns     int avRows = m - rows.size();     int avCols = n - cols.size();       // Minimum number of choices we have     int choices = min(avRows, avCols);       // If number of choices are odd     if (choices & 1)           // P1 will finish last         cout << "P1";       // Otherwise, P2 will finish last     else         cout << "P2"; }   // Given matrix int main() {     int mat[][3]         = { { 1, 0, 0 }, { 0, 0, 0 }, { 0, 0, 1 } };       findLast(mat); }   // This code is contributed by ukasp.

## Java

 // Java program for the above approach import java.util.*; import java.lang.*;   class GFG{     // Function to find the person // who will finish last static void findLast(int mat[][]) {     int m = 3;     int n = 3;       // To keep track of rows     // and columns having 1     Set rows = new HashSet();     Set cols = new HashSet();           for(int i = 0; i < m; i++)     {         for(int j = 0; j < n; j++)          {             if ((mat[i][j] > 0))             {                 rows.add(i);                 cols.add(j);             }         }     }       // Available rows and columns     int avRows = m - rows.size();     int avCols = n - cols.size();       // Minimum number of choices we have     int choices = Math.min(avRows, avCols);       // If number of choices are odd     if ((choices & 1) != 0)           // P1 will finish last         System.out.println("P1");       // Otherwise, P2 will finish last     else         System.out.println("P2"); }   // Driver code public static void main (String[] args) {     int mat[][] = { { 1, 0, 0 },                      { 0, 0, 0 },                      { 0, 0, 1 } };                           findLast(mat); } }   // This code is contributed by jana_sayantan

## Python3

 # Python3 program for the above approach   # Function to find the person # who will finish last def findLast(mat):       m = len(mat)     n = len(mat[0])       # To keep track of rows     # and columns having 1     rows = set()     cols = set()       for i in range(m):         for j in range(n):             if mat[i][j]:                 rows.add(i)                 cols.add(j)       # Available rows and columns     avRows = m-len(list(rows))     avCols = n-len(list(cols))       # Minimum number of choices we have     choices = min(avRows, avCols)       # If number of choices are odd     if choices & 1:               # P1 will finish last         print('P1')       # Otherwise, P2 will finish last     else:         print('P2')     # Given matrix mat = [[1, 0, 0], [0, 0, 0], [0, 0, 1]]   findLast(mat)

## C#

 // C# program for the above approach using System; using System.Collections.Generic;   class GFG{       // Function to find the person // who will finish last static void findLast(int[,] mat) {     int m = 3;     int n = 3;        // To keep track of rows     // and columns having 1     HashSet<int> rows = new HashSet<int>();     HashSet<int> cols = new HashSet<int>();            for(int i = 0; i < m; i++)     {         for(int j = 0; j < n; j++)         {             if ((mat[i,j] > 0))             {                 rows.Add(i);                 cols.Add(j);             }         }     }        // Available rows and columns     int avRows = m - rows.Count;     int avCols = n - cols.Count;        // Minimum number of choices we have     int choices = Math.Min(avRows, avCols);        // If number of choices are odd     if ((choices & 1) != 0)            // P1 will finish last         Console.WriteLine("P1");        // Otherwise, P2 will finish last     else         Console.WriteLine("P2"); }    // Driver code static public void Main() {           int[,] mat = { { 1, 0, 0 },                    { 0, 0, 0 },                    { 0, 0, 1 } };                        findLast(mat); } }   // This code is contributed by avanitrachhadiya2155

## Javascript

 

Output:

P1

Time Complexity: O(M*N)
Auxiliary Space: O(M*N)

Article Tags :
Practice Tags :
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/find-the-person-who-will-finish-last/?ref=ml_lbp", "fetch_time": 1716812670000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2024-22/segments/1715971059040.32/warc/CC-MAIN-20240527113621-20240527143621-00883.warc.gz", "warc_record_offset": 677904105, "warc_record_length": 47334, "token_count": 1853, "char_count": 6012, "score": 3.78125, "int_score": 4, "crawl": "CC-MAIN-2024-22", "snapshot_type": "latest", "language": "en", "language_score": 0.7852734923362732, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00056-of-00064.parquet"}