Difficulty Level: Easy

Total Submissions: 6933 Accuracy:


The Celebrity Problem (Function Problem)

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 there is an edge from ith person to jth person. An edge represent the relation that 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.

The first line of input contains an element T denoting the No 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.
For each test case output will be the id of the celebrity if present (0 based index). Else -1 will be printed. 


Input (To be used only for expected output) 

0 1 0 0 0 0 0 1 0


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 

Note:The Input/Ouput format and Example given are used for system's internal purpose, and should be used by a user for Expected Output only. As it is a function problem, hence a user should not read any input from stdin/console. The task is to complete the function specified, and not to write the full code.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Amit Khandelwal, Harshit Sidhwa

It is recommended to 'Compile & Test' your code before clicking 'Submit'!

Compilation/Execution Result:

Need help with your code? Please use, generate link and share the link here.