Given **n x n** points on cartesian plane, the task is to find the weight at **(x _{i}, y_{j})** after

**m**operation.

**x**denotes the operation of adding weight

_{i}, y_{j}, w**w**at all the points on the lines

**x = x**.

_{i}and y = y_{j}**Examples:**

Input:n = 3, m = 2

0 0 1

1 1 2

x = 1, y = 0

Output:3

Explanation:Initially, weights are

0 0 0

0 0 0

0 0 0

After 1st operation

2 1 1

1 0 0

1 0 0

After 2nd operation

2 3 1

3 4 2

1 0 0

Clearly, weight at (x_{1}, y_{0}) is 3

Input:n = 2, m = 2

0 1 1

1 0 2

x = 1, y = 1

Output:3

**Naive Approach:**

Consider a 2-d array arr[n][n] = {0} and perform the given operations and then, retrieve the weight at **(x _{i}, y_{j})**. This approach will take

**O(n*m)**time.

**Efficient Approach:**

- Consider the arrays arrX[n] = arrY[n] = {0}.
- Redefine the operation x
_{i}, y_{j}, w asarrX[i] += w and arrY[j] += w

- Find weight at (x
_{i}, y_{j}) usingw = arrX[i] + arrY[j]

Below is the implementation of the above approach:

## C++

`// C++ program to find the ` `// weight at xi and yi ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate weight at (xFind, yFind) ` `int` `findWeight(vector<vector<` `int` `> >& operations, ` ` ` `int` `n, ` `int` `m, ` ` ` `int` `xFind, ` `int` `yFind) ` `{ ` ` ` `int` `row[n] = { 0 }; ` ` ` `int` `col[n] = { 0 }; ` ` ` ` ` `// Loop to perform operations ` ` ` `for` `(` `int` `i = 0; i < m; i++) { ` ` ` ` ` `// Updating row ` ` ` `row[operations[i][0]] ` ` ` `+= operations[i][2]; ` ` ` ` ` `// Updating column ` ` ` `col[operations[i][0]] ` ` ` `+= operations[i][2]; ` ` ` `} ` ` ` ` ` `// Find weight at (xi, yj) using ` ` ` `// w = arrX[i] + arrY[j] ` ` ` `int` `result = row[xFind] + col[yFind]; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<vector<` `int` `> > operations ` ` ` `= { ` ` ` `{ 0, 0, 1 }, ` ` ` `{ 1, 1, 2 } ` ` ` `}; ` ` ` `int` `n = 3, ` ` ` `m = operations.size(), ` ` ` `xFind = 1, ` ` ` `yFind = 0; ` ` ` `cout << findWeight(operations, ` ` ` `n, m, xFind, ` ` ` `yFind); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the ` `# weight at xi and yi ` ` ` `# Function to calculate weight at (xFind, yFind) ` `def` `findWeight(operations, n, m, xFind, yFind) : ` ` ` ` ` `row ` `=` `[ ` `0` `] ` `*` `n ` ` ` `col ` `=` `[ ` `0` `] ` `*` `n ` ` ` ` ` `# Loop to perform operations ` ` ` `for` `i ` `in` `range` `(m) : ` ` ` ` ` `# Updating row ` ` ` `row[operations[i][` `0` `]]` `+` `=` `operations[i][` `2` `] ` ` ` ` ` `# Updating column ` ` ` `col[operations[i][` `0` `]]` `+` `=` `operations[i][` `2` `] ` ` ` ` ` `# Find weight at (xi, yj) using ` ` ` `# w = arrX[i] + arrY[j] ` ` ` `result ` `=` `row[xFind] ` `+` `col[yFind] ` ` ` ` ` `return` `result ` ` ` `# Driver code ` `operations ` `=` `[[ ` `0` `, ` `0` `, ` `1` `],[ ` `1` `, ` `1` `, ` `2` `]] ` `n ` `=` `2` `m ` `=` `len` `(operations) ` `xFind ` `=` `1` `yFind ` `=` `0` `print` `(findWeight(operations,n, m, xFind, yFind)) ` ` ` `# This code is contributed by divyamohan123 ` |

*chevron_right*

*filter_none*

**Output:**

3

**Time Complexity:** where m is the number of operations

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Print the array after K operations
- Minimum steps to reach end from start by performing multiplication and mod operations with array elements
- Minimum operations to make sum of neighbouring elements <= X
- Minimize operations required to make each element of Array equal to it's index value
- Count of elements of an array present in every row of NxM matrix
- How to access elements of a Square Matrix
- Pair with maximum sum in a Matrix
- Pair with maximum difference in a Matrix
- Problem of 8 Neighbours of an element in a 2-D Matrix
- Find duplicate in an array in O(n) and by using O(1) extra space
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N
- Find k pairs with smallest sums in two arrays | Set 2
- Find the largest number that can be formed with the given digits
- Find the maximum length of the prefix
- Find the smallest positive number missing from an unsorted array | Set 3
- Find the missing number in unordered Arithmetic Progression
- Find indices of all local maxima and local minima in an Array

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.