The below algorithm is used to transform the matrix and we will use it for Matrix Summation. We need to find the original matrix, given the transformed matrix.
The algorithm used for the transformation
v = 0
for i in range(x+1):
for j in range(y+1):
v += before(i,j)
after(x,y) = v
This algorithm will run for every x and y in the original matrix.
Example
before(0,0) => before(0,0)
before(0,1) => before(0,0) + before(0,1)
before(1,0) => before(0,0) + before(1,0)
before(1,1) => before(0,0) + before(0,1) + before(1,0) + before(1,1)
In, this way the matrix is transformed.
Our task is to write a function, that takes the transformed matrix as input and returns the original matrix. This can also be termed as decoding a matrix.
Approach
The transforming algorithm adds all the previous elements of that row and column. To get back to the original matrix, we need to subtract from previous values and add the previous diagonal values. We just need to take care of the appropriate boundary conditions.
Algorithm
for(i=x-1; i<=0; i--){
for(j=y-1; j<=0; j--){
if(i-1 >= 0 && j-1 >= 0){
transfromedMatrix[i][j] = transfromedMatrix[i][j] - transfromedMatrix[i-1][j] - transfromedMatrix[i][j-1] + transfromedMatrix[i-1][j-1]
}
else if(j-1 >= 0){
transfromedMatrix[i][j] -= transfromedMatrix[i][j-1]
}
else if(i-1 >= 0){
transfromedMatrix[i][j] -= transfromedMatrix[i-1][j]
}
Python Solution for Matrix Summation Hackerrank Solution
def getOriginalMatrix(transformedMatrix):
x = len(transformedMatrix)
y = len(transformedMatrix[0])
for i in range(x-1, -1, -1):
for j in range(y-1, -1, -1):
if i-1 >= 0 and j-1 >= 0:
transformedMatrix[i][j] = transformedMatrix[i][j] - transformedMatrix[i-1][j] - transformedMatrix[i][j-1] + transformedMatrix[i-1][j-1]
elif j-1 >= 0:
transformedMatrix[i][j] -= transformedMatrix[i][j-1]
elif i-1 >= 0:
transformedMatrix[i][j] -= transformedMatrix[i-1][j]
return transformedMatrix
Also Read:
- Download 1000+ Projects, All B.Tech & Programming Notes, Job, Resume & Interview Guide, and More – Get Your Ultimate Programming Bundle!
- HackerRank Day 8 Solution in Python: Dictionaries and Maps
- HackerRank Day 7 Solution in Python: Arrays
- HackerRank Day 6 Solution in Python: Let’s review
- HackerRank Day 5 Solution in Python: Loops
- HackerRank Day 4 Solution in Python: Class vs Instance
- HackerRank Day 3 Solution in Python: Intro to Conditional Statements
- HackerRank Day 2 Solution in Python: Operators
- HackerRank Day 1 Solution in Python: Data Types
- HackerRank Day 0 Solution in Python: Hello World
- HackerRank Day 29 Solution in Python: Bitwise AND
- HackerRank Day 28 Solution in Python: RegEx, Patterns, and Intro to databases
- HackerRank Day 27 Solution in Python: Testing
- HackerRank Day 26 Solution in Python: Nested Logic
- HackerRank Day 25 Solution in Python: Running Time and Complexity
- HackerRank Day 24 Solution in Python: More Linked Lists
- HackerRank Day 23 Solution in Python: BST Level Order Traversal
- HackerRank Day 22 Solution in Python: Binary Search Trees
- Find Peak Element LeetCode 162
- HackerRank Day 20 Solution in Python: Sorting
- HackerRank Day 19 Solution in Python: Interfaces
- HackerRank Day 18 Solution in Python: Queues and Stacks
- HackerRank Day 17 Solution in Python: More Exceptions
- HackerRank Day 16 Solution: Exceptions – String to Integer
- Explained: Allocate minimum number of pages
- HackerRank Day 15 Solution in Python: Linked List
- Search a 2D matrix: leetcode 74
- Maximum Subarray Sum: Kadane’s Algorithm
- HackerRank Day 13 Solution in Python: Abstract Classes
- HackerRank Day 14 Solution in Python: Scope