# 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[0][2], matrix[1][0] and matrix[2][1] meet the criteria.

Example 2

Input

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

Output

1

Explanation

Only matrix[0][2] 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[0].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[0].length; i++)
if (matrix[0][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[0])
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```
```

## Polynomial Division

Consider a sequence, c0, c1, . . . , cn-1 , and a polynomial of degree 1 defined as Q(x ) = a * x + b. You must perform q queries on the sequence, where each query is one of the following two types: 1 i x: Replace ci with x. 2 l r: Consider the polynomial and determine whether is divisible by over the field , where . In other words, check if there exists a polynomial with integer coefficie

## Costly Intervals

Given an array, your goal is to find, for each element, the largest subarray containing it whose cost is at least k. Specifically, let A = [A1, A2, . . . , An ] be an array of length n, and let be the subarray from index l to index r. Also, Let MAX( l, r ) be the largest number in Al. . . r. Let MIN( l, r ) be the smallest number in Al . . .r . Let OR( l , r ) be the bitwise OR of the

## The Strange Function

One of the most important skills a programmer needs to learn early on is the ability to pose a problem in an abstract way. This skill is important not just for researchers but also in applied fields like software engineering and web development. You are able to solve most of a problem, except for one last subproblem, which you have posed in an abstract way as follows: Given an array consisting

## Self-Driving Bus

Treeland is a country with n cities and n - 1 roads. There is exactly one path between any two cities. The ruler of Treeland wants to implement a self-driving bus system and asks tree-loving Alex to plan the bus routes. Alex decides that each route must contain a subset of connected cities; a subset of cities is connected if the following two conditions are true: There is a path between ever

## Unique Colors

You are given an unrooted tree of n nodes numbered from 1 to n . Each node i has a color, ci. Let d( i , j ) be the number of different colors in the path between node i and node j. For each node i, calculate the value of sum, defined as follows: Your task is to print the value of sumi for each node 1 <= i <= n. Input Format The first line contains a single integer, n, denoti

## Fibonacci Numbers Tree

Shashank loves trees and math. He has a rooted tree, T , consisting of N nodes uniquely labeled with integers in the inclusive range [1 , N ]. The node labeled as 1 is the root node of tree , and each node in is associated with some positive integer value (all values are initially ). Let's define Fk as the Kth Fibonacci number. Shashank wants to perform 22 types of operations over his tree, T