Cube of a matrix
Given an M x N sized 2D array 'MATRIX', return the (i * i + j * j) value for elements in which the sum of cube of digits of the element is equal to the element itself. Here, 'i' is the row number from ‘0’ to ‘M-1’, and 'j' is the column number from ‘0’ to ‘N-1’.
Note:
If no such number is present, return -1.
Input format:
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next ‘2T’ lines represent the ‘T’ test cases.
The first line of each test case contains ‘M’ and ‘N’, denoting the number of rows and columns, respectively.
The second line of each test case contains ‘M’ * ’N’ space-separated integers representing the elements of the 2D array 'MATRIX'.
Output format:
For each test case, return (i * i + j * j) value for elements in which the sum of cube of digits of the element is equal to the element itself, where 'i' is the row number from ‘0’ to ‘M' - 1, and 'j' is the column number from ‘0’ to ‘N’ - 1.
Note:
You are not required to print the output, it has already been taken care of. Just implement the function.
Constraints:
1 <= T <= 10
1 <= M <= 100
1 <= N <= 50
1 <= ELEMENT <= 10^9
Time limit: 1 second
CodingNinjas
author
2y
Brute Force approach
The idea is to traverse the matrix of size M x N. For each element in the matrix, we find the sum of the cube of their digits recursively. If the sum of the cube of digits is equal...read more
CodingNinjas
author
2y
Optimised approach
The idea is to traverse the matrix of size M x N. For each element in the matrix, we find the sum of the cube of their digits. If the sum of the cube of digits is equal to the elemen...read more
Help your peers!
Add answer anonymously...
Top 42Gears Mobility Systems Mts1 interview questions & answers
Popular interview questions of Mts1
>
42Gears Mobility Systems Mts1 Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app