Question: Deloitte National Level Assessment (NLA) | Nearest Number Matrix Operations | Feb 5th Slot | 2D Arrays & Mathematical Rounding Algorithms | Coding Round Solutions | Replace Matrix Elements
0
Entering edit mode

Question: Matrix Element Replacement (Nearest Divisible by 5)

Problem Statement:

Consider A is a square matrix with size N. Write a program to replace every element A[i][j] of the matrix with a number that is nearest to or equal to A[i][ j] divisible by 5.

Read the input from STDIN and write the output to STDOUT. You should not write arbitrary strings while reading the input and while printing as these contribute to the standard output.

Constraints:

  1. 1 < N < 50
  2. Each row of the output matrix should end with a single whitespace.

Input Format:

  • The first line of input contains an integer N, the size of the matrix.
  • The next N lines contain N integers each separated by a single whitespace.

Output Format:

  • The output should print the N \times N matrix, where each element is nearest or equal to A[i][j] and divisible by 5. Elements in each row should be separated by a single whitespace, and each row should end with a single whitespace.

Sample Input 1:

2

1 8

3 6

Sample Output 1:

0 10

5 5

Explanation 1:

From the Sample Input 1, we have:

2

1 8

3 6

  • For 1, the nearest multiple of 5 is 0.
  • For 8, the nearest multiple of 5 is 10.
  • For 3, the nearest multiple of 5 is 5.
  • For 6, the nearest multiple of 5 is 5.
    (Note: If a number is exactly exactly between two multiples, the nearest logic will apply based on standard rounding, though typically the problem expects standard absolute difference minimization).
ADD COMMENTlink 7 hours ago Sarthak • 0

Login before adding your answer.

Similar Posts
Loading Similar Posts