Count digits present in each element of a given Matrix
Last Updated :
07 Mar, 2022
Given a matrix arr[][] of dimensions M * N, the task is to count the number of digits of every element present in the given matrix.
Examples:
Input: arr[][] = { { 27, 173, 5 }, { 21, 6, 624 }, { 5, 321, 49 } }
Output:
2 3 1
2 1 3
1 3 2
Input: arr[][] = { {11, 12, 33 }, { 64, 57, 61 }, { 74, 88, 39 } }
Output:
2 2 2
2 2 2
2 2 2
Approach: The idea to solve this problem is to traverse the given matrix and for every index of the matrix, count the number of digits of the number present at that index using the following expression:
Number of digits present in any value X is given by floor(log10(X))+1.
Below is the implementation of the above approach:
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
const int M = 3;
const int N = 3;
// Function to count the number of digits
// in each element of the given matrix
void countDigit(int arr[M][N])
{
// Traverse each row of arr[][]
for (int i = 0; i < M; i++) {
// Traverse each column of arr[][]
for (int j = 0; j < N; j++) {
// Store the current matrix element
int X = arr[i][j];
// Count the number of digits
int d = floor(log10(X) * 1.0) + 1;
// Print the result
cout << d << " ";
}
cout << endl;
}
}
// Driver Code
int main()
{
// Given matrix
int arr[][3] = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
countDigit(arr);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG
{
static int M = 3;
static int N = 3;
// Function to count the number of digits
// in each element of the given matrix
static void countDigit(int arr[][])
{
// Traverse each row of arr[][]
for (int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for (int j = 0; j < N; j++)
{
// Store the current matrix element
int X = arr[i][j];
// Count the number of digits
int d = (int) (Math.floor(Math.log10(X) * 1.0) + 1);
// Print the result
System.out.print(d+ " ");
}
System.out.println();
}
}
// Driver Code
public static void main(String[] args)
{
// Given matrix
int arr[][] = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
countDigit(arr);
}
}
// This code is contributed by Princi Singh
Python3
# Python3 program for the above approach
from math import floor, log10
M = 3
N = 3
# Function to count the number of digits
# in each element of the given matrix
def countDigit(arr):
# Traverse each row of arr[][]
for i in range(M):
# Traverse each column of arr[][]
for j in range(N):
# Store the current matrix element
X = arr[i][j]
# Count the number of digits
d = floor(log10(X) * 1.0) + 1
# Print the result
print(d, end = " ")
print()
# Driver Code
if __name__ == '__main__':
# Given matrix
arr = [ [ 27, 173, 5 ],
[ 21, 6, 624 ],
[ 5, 321, 49 ] ]
countDigit(arr)
# This code is contributed by mohit kumar 29
C#
// C# program to implement
// the above approach
using System;
class GFG
{
static int M = 3;
static int N = 3;
// Function to count the number of digits
// in each element of the given matrix
static void countDigit(int[,] arr)
{
// Traverse each row of arr[][]
for (int i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for (int j = 0; j < N; j++)
{
// Store the current matrix element
int X = arr[i, j];
// Count the number of digits
int d = (int) (Math.Floor(Math.Log10(X) * 1.0) + 1);
// Print the result
Console.Write(d + " ");
}
Console.WriteLine();
}
}
// Driver Code
public static void Main()
{
// Given matrix
int[,] arr = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
countDigit(arr);
}
}
// This code is contributed by susmitakundugoaldanga
JavaScript
<script>
// JavaScript program to implement
// the above approach
let M = 3;
let N = 3;
// Function to count the number of digits
// in each element of the given matrix
function countDigit(arr)
{
// Traverse each row of arr[][]
for (let i = 0; i < M; i++)
{
// Traverse each column of arr[][]
for (let j = 0; j < N; j++)
{
// Store the current matrix element
let X = arr[i][j];
// Count the number of digits
let d = (Math.floor(Math.log10(X) * 1.0) + 1);
// Print the result
document.write(d+ " ");
}
document.write("<br/>");
}
}
// Driver code
// Given matrix
let arr = [[ 27, 173, 5 ],
[ 21, 6, 624 ],
[ 5, 321, 49 ]];
countDigit(arr);
</script>
Output: 2 3 1
2 1 3
1 3 2
Time Complexity: O(M * N)
Auxiliary Space: O(1)
Similar Reads
Common elements in all rows of a given matrix Given an m x n matrix, find all common elements present in all rows in O(mn) time and one traversal of matrix.Example: Input:mat[4][5] = {{1, 2, 1, 4, 8}, {3, 7, 8, 5, 1}, {8, 7, 7, 3, 1}, {8, 1, 2, 7, 9}, };Output: 1 8 or 8 18 and 1 are present in all rows.A simple solution is to consider every ele
7 min read
Count rows in a matrix that consist of same element Given a matrix mat[][], the task is to count the number of rows in the matrix that consists of the same elements. Examples: Input: mat[][] = {{1, 1, 1}, {1, 2, 3}, {5, 5, 5}} Output: 2 All the elements of the first row and all the elements of the third row are the same. Input: mat[][] = {{1, 2}, {4,
9 min read
Count of unique rows in a given Matrix Given a 2D matrix arr of size N*M containing lowercase English letters, the task is to find the number of unique rows in the given matrix. Examples: Input: arr[][]= { {'a', 'b', 'c', 'd'}, {'a', 'e', 'f', 'r'}, {'a', 'b', 'c', 'd'}, {'z', 'c', 'e', 'f'} }Output: 2Explanation: The 2nd and the 4th row
10 min read
Row-wise common elements in two diagonals of a square matrix Given a square matrix, find out count of numbers that are same in same row and same in both primary and secondary diagonals. Examples : Input : 1 2 1 4 5 2 0 5 1 Output : 2 Primary diagonal is 1 5 1 Secondary diagonal is 1 5 0 Two elements (1 and 5) match in two diagonals and same. Input : 1 0 0 0 1
4 min read
Find the count of mountains in a given Matrix Given a 2D square matrix of size N X N, the task is to count the number of mountains in the matrix. An element in a matrix is said to be a mountain when all the surrounding elements (in all 8 directions) are smaller than the given element. Examples: Input: matrix = { { 4, 5, 6 }, { 2, 1, 3 }, { 7, 8
9 min read