You are in a party of **N** people, where only one person is known to everyone. Such a person **may be present** in the party, if yes, **(s)he doesn’t know anyone **in the party. Your task is to find the stranger (celebrity) in party.

You will be given a square matrix **M**[][] where if an element of row i and column j is set to 1 it means i^{th} person knows j^{th} person. You need to complete the function **getId()** which finds the id of the celebrity if present else return **-1**. The function **getId**() takes two arguments, the square matrix **M** and its size **N**.

Here, **M[i][i]** will be equal to **zero**.

**Note:** Expected time complexity is O(N) with constant extra space.

**Input:**

The first line of input contains an element T denoting the number of test cases. Then T test cases follow. Each test case consist of 2 lines. The first line of each test case contains a number denoting the size of the matrix M. Then in the next line are space separated values of the matrix **M**.

**Output:**

For each test case output will be the id of the celebrity if present (0 based index). Else **-1** will be printed.

**User Task:**

The task is to complete the function **getId() **which returns the Id of celebrity if present, else **-1**.

**Constraints:**

1 <= T <= 50

2 <= N <= 501

0 <= M[][] <= 1

**Example:
Input **:

2

3

0 1 0 0 0 0 0 1 0

2

0 1 1 0

1

-1

**Explanation **:

**Testcase 1:**

For the above test case the matrix will look like

*0 1 0
0 0 0
0 1 0*

Here, the celebrity is the person with index 1 ie id 1

The matrix will look like

0 1

1 0

Here, there is

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

saiujwal13083 | 489 |

sudeep235poojary | 302 |

VithalNakod | 272 |

AshutoshPal1 | 254 |

go__karuna | 253 |

Soseph_Jtalin | 792 |

AshutoshPal1 | 722 |

architjindal__ | 648 |

go__karuna | 577 |

ArbazGrewal | 527 |

blackshadows | 5362 |

Ibrahim Nash | 5242 |

akhayrutdinov | 5111 |

mb1973 | 4931 |

Quandray | 4598 |

Login to report an issue on this page.