# Count frequency of k in a matrix of size n where matrix(i, j) = i+j

Given a matrix of size n*n. Count the frequency of given element k in that matrix. **Here base index is 1.**

Examples:

Input : n = 4, k = 7 Output : 2ExplanationThe matrix will be 2 3 4 5 3 4 5 6 4 5 6 7 5 6 7 8 in the given matrix where M(i, j) = i+j, frequency of 7 is 2 Input : n = 5, k = 4 Output : 3ExplanationThe matrix will be 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9 6 7 8 9 10ExplanationIn the given matrix where M(i, j) = i+j, frequency of 4 is 3

**First method**

1) Construct a matrix of size n*n.

2) Fill the value with M(i, j)=i+j.(recall that here base index is 1)

3) Iteratively traverse the matrix and and count the frequency of given element.

This method is not that much efficient because if the matrix size is very large it’s will result in Time limit exceed.time complexity will be O(n^2).

**Efficient method**

In this method we avoid creating matrix of size n*n.

for example

if n = 10 the matrix will be

2 3 4 5 6 7 8 9 10 11 3 4 5 6 7 8 9 10 11 12 4 5 6 7 8 9 10 11 12 13 5 6 7 8 9 10 11 12 13 14 6 7 8 9 10 11 12 13 14 15 7 8 9 10 11 12 13 14 15 16 8 9 10 11 12 13 14 15 16 17 9 10 11 12 13 14 15 16 17 18 10 11 12 13 14 15 16 17 18 19 11 12 13 14 15 16 17 18 19 20

Now, notice how the values are same in the secondary Diagonal, and we can also find a pattern in the count it increases like 1, 2, 3, 4,

here we can see that

if (n+1)>=k then frequency of k is k-1

else frequency will be 2*n+1-k

## CPP

`// CPP program to find the frequency of k ` `// in matrix where m(i, j)=i+j ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `int` `find(` `int` `n, ` `int` `k) ` `{ ` ` ` `if` `(n + 1 >= k) ` ` ` `return` `(k - 1); ` ` ` `else` ` ` `return` `(2 * n + 1 - k); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `n = 4, k = 7; ` ` ` `int` `freq = find(n, k); ` ` ` `if` `(freq < 0) ` ` ` `cout << ` `" element not exist \n "` `; ` ` ` `else` ` ` `cout << ` `" Frequency of "` `<< k ` ` ` `<< ` `" is "` `<< freq << ` `"\n"` `; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the ` `// frequency of k in matrix ` `// in matrix where m(i, j)=i+j ` `import` `java.util.*; ` `import` `java.lang.*; ` ` ` `public` `class` `GfG{ ` ` ` ` ` `public` `static` `int` `find(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `if` `(n + ` `1` `>= k) ` ` ` `return` `(k - ` `1` `); ` ` ` `else` ` ` `return` `(` `2` `* n + ` `1` `- k); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `main(String argc[]) ` ` ` `{ ` ` ` `int` `n = ` `4` `, k = ` `7` `; ` ` ` `int` `freq = find(n, k); ` ` ` `if` `(freq < ` `0` `) ` ` ` `System.out.print(` `" element"` ` ` `+ ` `" not exist \n "` `); ` ` ` `else` ` ` `System.out.print(` `" Frequency"` ` ` `+ ` `" of "` `+ k + ` `" is "` `+ ` ` ` `freq + ` `"\n"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sagar Shukla ` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find ` `# the frequency of k ` `# in matrix where ` `# m(i, j)=i+j ` ` ` `import` `math ` ` ` `def` `find( n, k): ` ` ` ` ` `if` `(n ` `+` `1` `>` `=` `k): ` ` ` `return` `(k ` `-` `1` `) ` ` ` `else` `: ` ` ` `return` `(` `2` `*` `n ` `+` `1` `-` `k) ` ` ` ` ` `# Driver Code ` `n ` `=` `4` `k ` `=` `7` ` ` `freq ` `=` `find(n, k) ` ` ` `if` `(freq < ` `0` `): ` ` ` `print` `( ` `" element not exist"` `) ` `else` `: ` ` ` `print` `(` `" Frequency of "` `, k ,` `" is "` `, freq ) ` ` ` `# This code is contributed ` `# by Gitanjali. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the ` `// frequency of k in matrix ` `// in matrix where m(i, j)=i+j ` `using` `System; ` ` ` `public` `class` `GfG{ ` ` ` ` ` `public` `static` `int` `find(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `if` `(n + 1 >= k) ` ` ` `return` `(k - 1); ` ` ` `else` ` ` `return` `(2 * n + 1 - k); ` ` ` `} ` ` ` ` ` `// Driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 4, k = 7; ` ` ` `int` `freq = find(n, k); ` ` ` `if` `(freq < 0) ` ` ` `Console.WriteLine(` `" element"` ` ` `+ ` `" not exist "` `); ` ` ` `else` ` ` `Console.WriteLine(` `" Frequency"` ` ` `+ ` `" of "` `+ k + ` `" is "` `+ ` ` ` `freq ); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find the frequency of k ` `// in matrix where m(i, j)=i+j ` ` ` `function` `find(` `$n` `, ` `$k` `) ` `{ ` ` ` `if` `(` `$n` `+ 1 >= ` `$k` `) ` ` ` `return` `(` `$k` `- 1); ` ` ` `else` ` ` `return` `(2 * ` `$n` `+ 1 - ` `$k` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 4; ` ` ` `$k` `= 7; ` ` ` `$freq` `= find(` `$n` `, ` `$k` `); ` ` ` `if` `(` `$freq` `< 0) ` ` ` `echo` `" element not exist \n "` `; ` ` ` `else` ` ` `echo` `" Frequency of "` `, ` `$k` ` ` `, ` `" is "` `, ` `$freq` `, ` `"\n"` `; ` ` ` `// This code is contributed by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Frequency of 7 is 2

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:

- Maximum size square sub-matrix with all 1s
- Print a given matrix in spiral form
- Search in a row wise and column wise sorted matrix
- A Boolean Matrix Question
- Matrix Chain Multiplication | DP-8
- Print unique rows in a given boolean matrix
- Inplace (Fixed space) M x N size matrix transpose | Updated
- Maximum sum rectangle in a 2D matrix | DP-27
- Zigzag (or diagonal) traversal of Matrix
- Divide and Conquer | Set 5 (Strassen's Matrix Multiplication)
- Print all possible paths from top left to bottom right of a mXn matrix
- Count all possible paths from top left to bottom right of a mXn matrix
- Printing brackets in Matrix Chain Multiplication Problem
- Create a matrix with alternating rectangles of O and X
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Print all elements in sorted order from row and column wise sorted matrix
- Given a matrix of 'O' and 'X', find the largest subsquare surrounded by 'X'
- Given a matrix of ‘O’ and ‘X’, replace 'O' with 'X' if surrounded by 'X'
- Find a common element in all rows of a given row-wise sorted matrix
- Count number of islands where every island is row-wise and column-wise separated

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.