# Count entries equal to x in a special matrix

You are given a square matrix (matrix[][]) of order n, where matrix[i][j] = i*j. Find the number of cells which have entry equal to a given number x.

NOte : Indexing of matrix starts from 1, i.e. 1<= i,j <= n.

**Examples :**

Input : matrix[][] = {1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16} x = 4 Output : 3 Input : matrix[][] = {1, 2, 3, 4, 2, 4, 6, 8, 3, 6, 9, 12, 4, 8, 12, 16} x = 12 Output : 2

A **simple Approach** is to traverse the whole of matrix and check whether cell value is equal to given x and then increase count value accordingly. Time complexity in this approach is quite high and is equal to O(n^2).

// traverse and check whole matrix for (int i=1; i<=n ; i++) { for (int j=1; j<=n; j++) if (i * j == x) count++; } // return count return count;

An **efficient approach** is to only find the number of factors of given x in the range 0 to x and also those factors (including divisor and quotient ) must be less than or equal to n (order of matrix). In this case time complexity will be O(n).

// traverse and find the factors for (int i=1; i<=n && i<=x ; i++) { // x%i == 0 means i is factor of x // x/i <= n means i and j are <= n (for i*j=x) if ( x/i <= n && x%i ==0) count++; } // return count return count;

## C++

`// CPP program for counting number of cell ` `// equals to given x ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to count factors as number of cell ` `int` `count (` `int` `n, ` `int` `x) ` `{ ` ` ` `int` `count == 0; ` ` ` `// traverse and find the factors ` ` ` `for` `(` `int` `i=1; i<=n && i<=x ; i++) ` ` ` `{ ` ` ` `// x%i == 0 means i is factor of x ` ` ` `// x/i <= n means i and j are <= n (for i*j=x) ` ` ` `if` `( x/i <= n && x%i ==0) ` ` ` `count++; ` ` ` `} ` ` ` `// return count ` ` ` `return` `count; ` `} ` ` ` `// driver program ` `int` `main() ` `{ ` ` ` `int` `n = 8; ` ` ` `// we can manually assume matrix of order 8*8 ` ` ` `// where mat[i][j] = i*j , 0<i,j<=n ` ` ` `int` `x = 24; ` ` ` `cout << count(n, x); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program for counting number of ` `// cell equals to given x ` `class` `GFG ` `{ ` ` ` `// function to count factors as ` ` ` `// number of cell ` ` ` `static` `int` `count (` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` `int` `count = ` `0` `; ` ` ` ` ` `// traverse and find the factors ` ` ` `for` `(` `int` `i = ` `1` `; i <= n && i <= x ; ` ` ` `i++) ` ` ` `{ ` ` ` `// x%i == 0 means i is factor ` ` ` `// of x. x/i <= n means i and ` ` ` `// j are <= n (for i*j=x) ` ` ` `if` `( x / i <= n && x % i == ` `0` `) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// return count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `int` `n = ` `8` `; ` ` ` ` ` `// we can manually assume matrix ` ` ` `// of order 8*8 where ` ` ` `// mat[i][j] = i*j , 0<i,j<=n ` ` ` `int` `x = ` `24` `; ` ` ` `System.out.println(count(n, x)); ` ` ` `} ` `} ` ` ` `/*This code is contributed by Danish kaleem*/` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program for counting ` `# number of cell equals to given x ` ` ` `# function to count factors ` `# as number of cell ` `def` `count(n, x): ` ` ` `cnt ` `=` `0` ` ` ` ` `# traverse and find the factors ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# // x%i == 0 means i is factor of x ` ` ` `# x/i <= n means i and j are <= n (for i*j=x) ` ` ` `if` `i <` `=` `x: ` ` ` `if` `x ` `/` `/` `i <` `=` `n ` `and` `x ` `%` `i ` `=` `=` `0` `: ` ` ` `cnt ` `+` `=` `1` ` ` `return` `cnt ` ` ` `# Driver code ` `n ` `=` `8` `x ` `=` `24` `print` `(count(n, x)) ` ` ` `# This code is contributed by Shrikant13 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for counting number ` `// of cell equals to given x ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// function to count factors as ` ` ` `// number of cell ` ` ` `static` `int` `count (` `int` `n, ` `int` `x) { ` ` ` ` ` `int` `count = 0; ` ` ` ` ` `// traverse and find the factors ` ` ` `for` `(` `int` `i = 1; i <= n && ` ` ` `i <= x ; i++) ` ` ` `{ ` ` ` ` ` `// x%i == 0 means i is factor ` ` ` `// of x. x/i <= n means i and ` ` ` `// j are <= n (for i*j=x) ` ` ` `if` `( x / i <= n && x % i == 0) ` ` ` `count++; ` ` ` `} ` ` ` ` ` `// return count ` ` ` `return` `count; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 8; ` ` ` ` ` `// we can manually assume matrix ` ` ` `// of order 8*8 where ` ` ` `// mat[i][j] = i*j , 0<i,j<=n ` ` ` `int` `x = 24; ` ` ` `Console.Write(count(n, x)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nitin Mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program for counting ` `// number of cell equals to ` `// given x ` ` ` `// function to count factors ` `// as number of cell ` `function` `c_ount ( ` `$n` `, ` `$x` `) ` `{ ` ` ` `$Count` `= 0; ` ` ` `// traverse and find the factors ` ` ` `for` `( ` `$i` `= 1; ` `$i` `<= ` `$n` `and` ` ` `$i` `<= ` `$x` `; ` `$i` `++) ` ` ` `{ ` ` ` `// x%i == 0 means i is ` ` ` `// factor of x x/i <= n ` ` ` `// means i and j are ` ` ` `// <= n (for i*j=x) ` ` ` `if` `( ` `$x` `/ ` `$i` `<= ` `$n` `and` ` ` `$x` `% ` `$i` `== 0) ` ` ` `$Count` `++; ` ` ` `} ` ` ` `// return count ` ` ` `return` `$Count` `; ` `} ` ` ` `// Driver Code ` `$n` `= 8; ` ` ` `// we can manually assume ` `// matrix of order 8*8 ` `// where mat[i][j] = i*j , ` `// 0<i,j<=n ` `$x` `= 24; ` `echo` `c_ount(` `$n` `, ` `$x` `); ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

4

This article is contributed by **Shivam Pradhan (anuj_charm)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count elements smaller than or equal to x in a sorted matrix
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Maximum sub-matrix area having count of 1's one more than count of 0's
- Largest sub-matrix with all equal elements
- Place N^2 numbers in matrix such that every row has an equal sum
- Making all elements of matrix equal to a given element K
- Largest area rectangular sub-matrix with equal number of 1's and 0's
- Find the largest area rectangular sub-matrix whose sum is equal to k
- Finding the maximum square sub-matrix with all equal elements
- Find number of transformation to make two Matrix Equal
- Minimum operations of given type to make all elements of a matrix equal
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Ways of filling matrix such that product of all rows and all columns are equal to unity
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Fill missing entries of a magic square
- Count of submatrix with sum X in a given Matrix
- Count all 0s which are blocked by 1s in binary matrix
- Count majority element in a matrix
- Count all sorted rows in a matrix
- Count of palindromic plus paths in a given Matrix