Compress a matrix into an integer using digit operations

Updated

Given a matrix mat[] of dimension N x M. The task is to compress the matrix into an array by applying the given operations on the columns then the array into an integer using the given operation.

In an operation between two elements, we consider two corresponding digits of the elements (a,b) if both of them are even or both of them are odd then we replace the digit by (a+b)%10 else we replace the digit by abs(a-b).

For example, for 857 and 485 will result in 232 ( (8+4)%10 = 2, abs(5-8) = 3, (7+5)%10 = 2 )

Examples :

Input : mat[] = { { 45 , 32, 67},
{ 89, 11, 54 },
{ 65, 43, 22 } }
Output : 54
Explanation :
arr[0] = 45, 89, 65 --> 24, 65 --> 81
arr[1] = 32, 11, 43 --> 41, 43 --> 84
arr[2] = 67, 54, 22 --> 13, 22 --> 11
Answer = 81, 84, 11 --> 63, 11 --> 54

Input : { { 395, 754, 537 },
{ 460, 224, 239 },
{ 355, 807, 229 },
{ 803, 81, 673 } }
Output : 645

Approach :

The idea is to convert the elements into a string and replace them after each operation,

  1. Traverse each row and convert the matrix into an array.
  2. Apply the operation on the first two elements, then on the result, and the next element.
  3. Convert the elements into a string and traverse them from the back and replace the digits.
  4. If two elements have an unequal number of digits then we pad the number with less number of digits by zeroes.

Implementation :

# Python implementation

# Function of apply operation between two integers
def oper(a,b):
  a = str(a)
  b = str(b)
  digA = len(a)
  digB = len(b)
  ans = ''
  
  # Traversing the digits of a and b
  while digA and digB:
    # Take (a+b)%10
    # If both the digits are odd
    if int(a[digA-1])&1 and int(b[digB-1])&1:
      ans += str((int(a[digA-1])+int(b[digB-1]))%10)
    
    # If both the digits are even
    elif not int(a[digA-1])&1 and not int(b[digB-1])&1:
      ans += str((int(a[digA-1])+int(b[digB-1]))%10)
    
    # Take |a - b| if the digits are different
    else:
      ans += str(abs(int(a[digA-1])-int(b[digB-1])))
      
    digA, digB = digA-1, digB-1
  
  # If the number of digits are not same
  while digA:
    ans += a[digA-1]
    digA -= 1
    
  while digB:
    ans += b[digB-1]
    digB -= 1
    
  return int(ans[::-1])
                 
# Function to reduce an array into an integer
def arrToInt(arr):
  ans = arr[0]
  
  # Traversing the array and applying operation
  for i in arr[1:]:
    ans = oper(ans,i)
  
  return ans

def getInt(mat):
  n,m = len(mat[0]),len(mat)
  
  arr = []
  # Get each column
  for i in range(n):
    getCol = []
    for j in range(m):
      getCol.append(mat[j][i])
    
    # Converting the column into an integer
    ele = arrToInt(getCol)

    arr.append(ele)
  
  ans = arrToInt(arr)
  
  return ans
    
      
# Driver Code
mat = [[45,32,67],[89,11,54],[65,43,22]]

# Function call
ans = getInt(mat)

# To display the result
print(ans)
Output

54

Time Complexity : O(N*M)

Comment
RKplus Published 30 Nov 2026 · 3 min read

Related tutorials