Find the weight at (Xi, Yi) after M operations in a Matrix
Last Updated :
10 Feb, 2024
Given n x n points on cartesian plane, the task is to find the weight at (xi, yj) after m operation.
xi, yj , w denotes the operation of adding weight w at all the points on the lines x = xi and y = yj.
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 (x1, y0) 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 (xi, yj). This approach will take O(n*m) time.
Efficient Approach:
Consider the arrays arrX[n] = arrY[n] = {0}.
Redefine the operation xi, yj, w as
arrX[i] += w
and
arrY[j] += w
Find weight at (xi, yj) using
w = arrX[i] + arrY[j]
Below is the implementation of the above approach:
-
C++
#include <bits/stdc++.h>
using namespace std;
int findWeight(vector<vector< int > >& operations,
int n, int m,
int xFind, int yFind)
{
int row[n] = { 0 };
int col[n] = { 0 };
for ( int i = 0; i < m; i++) {
row[operations[i][0]]
+= operations[i][2];
col[operations[i][0]]
+= operations[i][2];
}
int result = row[xFind] + col[yFind];
return result;
}
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;
}
|
Java
import java.util.*;
public class FindWeight {
static int findWeight(ArrayList<ArrayList<Integer>> operations,
int n, int m,
int xFind, int yFind) {
int [] row = new int [n];
int [] col = new int [n];
for ( int i = 0 ; i < m; i++) {
row[operations.get(i).get( 0 )] += operations.get(i).get( 2 );
col[operations.get(i).get( 1 )] += operations.get(i).get( 2 );
}
int result = row[xFind] + col[yFind];
return result;
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> operations = new ArrayList<>();
operations.add( new ArrayList<>(Arrays.asList( 0 , 0 , 1 )));
operations.add( new ArrayList<>(Arrays.asList( 1 , 1 , 2 )));
int n = 3 ;
int m = operations.size();
int xFind = 1 ;
int yFind = 0 ;
System.out.println(findWeight(operations, n, m, xFind, yFind));
}
}
|
Python3
def findWeight(operations, n, m, xFind, yFind) :
row = [ 0 ] * n
col = [ 0 ] * n
for i in range (m) :
row[operations[i][ 0 ]] + = operations[i][ 2 ]
col[operations[i][ 0 ]] + = operations[i][ 2 ]
result = row[xFind] + col[yFind]
return result
operations = [[ 0 , 0 , 1 ],[ 1 , 1 , 2 ]]
n = 2
m = len (operations)
xFind = 1
yFind = 0
print (findWeight(operations,n, m, xFind, yFind))
|
C#
using System;
public class WeightFinder
{
public static int FindWeight( int [][] operations, int n, int m, int xFind, int yFind)
{
int [] row = new int [n];
int [] col = new int [n];
for ( int i = 0; i < m; i++)
{
row[operations[i][0]] += operations[i][2];
col[operations[i][1]] += operations[i][2];
}
int result = row[xFind] + col[yFind];
return result;
}
public static void Main( string [] args)
{
int [][] operations =
{
new int [] { 0, 0, 1 },
new int [] { 1, 1, 2 }
};
int n = 3;
int m = operations.Length;
int xFind = 1;
int yFind = 0;
Console.WriteLine(FindWeight(operations, n, m, xFind, yFind));
}
}
|
Javascript
function findWeight(operations, n, m, xFind, yFind) {
let row = Array(n).fill(0);
let col = Array(n).fill(0);
for (let i = 0; i < m; i++) {
row[operations[i][0]] += operations[i][2];
col[operations[i][0]] += operations[i][2];
}
let result = row[xFind] + col[yFind];
return result;
}
let operations = [[0, 0, 1], [1, 1, 2]];
let n = 2;
let m = operations.length;
let xFind = 1;
let yFind = 0;
console.log(findWeight(operations, n, m, xFind, yFind));
|
- Output:
3
Time Complexity:-
- where m is the number of operations
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...