# Largest Elements in Their Row and Column - Google Top Interview Questions

### Problem Statement :

```You are given a two-dimensional list of integers matrix containing 1s and 0s. Return the number of elements in matrix such that:

matrix[r][c] = 1
matrix[r][j] = 0 for every j ≠ c and matrix[i][c] = 0 for every i ≠ r
Constraints

0 ≤ n, m ≤ 250 where n and m are the number of rows and columns in matrix

Example 1

Input

matrix = [
[0, 0, 1],
[1, 0, 0],
[0, 1, 0]
]

Output

3

Explanation

We have matrix, matrix and matrix meet the criteria.

Example 2

Input

matrix = [
[0, 0, 1],
[1, 0, 0],
[1, 0, 0]
]

Output

1

Explanation

Only matrix meet the criteria. The other two 1s share the same column.```

### Solution :

```                        ```Solution in C++ :

int solve(vector<vector<int>>& matrix) {
vector<int> row(matrix.size(), 0), col(matrix.size(), 0);
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
col[j] += matrix[i][j];
row[i] += matrix[i][j];
}
}
int ans = 0;
for (int i = 0; i < matrix.size(); i++) {
for (int j = 0; j < matrix[i].size(); j++) {
ans += (matrix[i][j] == 1 and row[i] == 1 and col[j] == 1);
}
}
return ans;
}```
```

```                        ```Solution in Java :

import java.util.*;

class Solution {
public int solve(int[][] matrix) {
int count = 0;
int count2 = 0;
int ans = 0;
int index = 0;
// edge case
if (matrix.length == 1) {
int c = 0;
for (int i = 0; i < matrix.length; i++)
if (matrix[i] == 1)
c++;
if (c == 1)
return 1;
else
return 0;
}
// First we check row-wise. If only Element found then only. We have to check column wise
// from 0 - to end index of the particular column.
for (int i = 0; i < matrix.length; i++) {
count = 0;
count2 = 0;
for (int j = 0; j < matrix[i].length; j++) {
if (matrix[i][j] == 1) {
count++;
index = j;
}
}

if (count != 1)
continue;
for (int k = 0; k < matrix.length; k++)
if (matrix[k][index] == 1)
count2++;
if (count == 1 && count2 == 1)
ans++;
}
return ans;
}
}```
```

```                        ```Solution in Python :

class Solution:
def solve(self, matrix):
if not matrix:
return 0

row = [sum(r) for r in matrix]
col = [sum(c) for c in zip(*matrix)]

m, n = len(matrix), len(matrix)
res = 0
for r in range(m):
for c in range(n):
if matrix[r][c] == 1 and row[r] == 1 and col[c] == 1:
res += 1
return res```
```

## Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

## Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

## Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

## Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your \$inOrder* func

## Tree: Height of a Binary Tree

The height of a binary tree is the number of edges between the tree's root and its furthest leaf. For example, the following binary tree is of height : image Function Description Complete the getHeight or height function in the editor. It must return the height of a binary tree as an integer. getHeight or height has the following parameter(s): root: a reference to the root of a binary

## Tree : Top View

Given a pointer to the root of a binary tree, print the top view of the binary tree. The tree as seen from the top the nodes, is called the top view of the tree. For example : 1 \ 2 \ 5 / \ 3 6 \ 4 Top View : 1 -> 2 -> 5 -> 6 Complete the function topView and print the resulting values on a single line separated by space.