- Related Questions & Answers
- Count elements smaller than or equal to x in a sorted matrix in C++
- Program to find X for special array with X elements greater than or equal X in Python
- Count numbers whose sum with x is equal to XOR with x in C++
- Count pairs in a binary tree whose sum is equal to a given value x in C++
- Count special palindromes in a String in C++
- Count pairs from two BSTs whose sum is equal to a given value x in C++
- Count sub-arrays which have elements less than or equal to X in C++
- Count elements such that there are exactly X elements with values greater than or equal to X in C++
- Count quadruples from four sorted arrays whose sum is equal to a given value x in C++
- Count pairs from two sorted arrays whose sum is equal to a given value x in C++
- Count triplets in a sorted doubly linked list whose sum is equal to a given value x in C++
- Count triplets in a sorted doubly linked list whose product is equal to a given value x in C++
- Count distinct entries in SAP BusinessObjects
- Select entries with timestamp after X time in MySQL
- Count all sorted rows in a matrix in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Given a square matrix, mat[][] let the elements of the matrix me mat[i][j] = i*j, the task is to count the number of elements in the matrix is equal to x.

Matrix is like a 2d array in which numbers or elements are represented as rows and columns.

So, let's understand the solution of the problem with the help of examples −

**Input** −

matrix[row][col] = { {1, 2, 3}, {3, 4, 3}, {3, 4, 5}}; x = 3

**Output** −

Count of entries equal to x in a special matrix: 4

**Input** −

matrix[row][col] = { {10, 20, 30}, {30, 40, 30}, {30, 40, 50}}; x = 30

**Output** −

Count of entries equal to x in a special matrix: 4

Take a matrix mat[][] and x as the input values.

In function count, we will count the number of entries.

Traverse the whole matrix, where you find the value of mat[i][j] == x then increment the count by 1.

Return the value of count and print it as a result.

#include<bits/stdc++.h> using namespace std; #define row 3 #define col 3 //count the entries equal to X int count (int matrix[row][col], int x){ int count = 0; // traverse and find the factors for(int i = 0 ;i<row;i++){ for(int j = 0; j<col; j++){ if(matrix[i][j] == x){ count++; } } } // return count return count; } int main(){ int matrix[row][col] = { {1, 2, 3}, {3, 4, 3}, {3, 4, 5} }; int x = 3; cout<<"Count of entries equal to x in a special matrix: "<<count(matrix, x); return 0; }

If we run the above code we will get the following output −

Count of entries equal to x in a special matrix: 4

Advertisements