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 : 2
Explanation
The 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 : 3
Explanation
The 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 10
Explanation
In the given matrix where M(i, j) = i+j,
frequency of 4 is 3
First method
- Construct a matrix of size n*n.
- Fill the value with M(i, j)=i+j.(recall that here base index is 1)
- Iteratively traverse the matrix and count the frequency of a given element.
This method is not that much efficient because if the matrix size is very large it’s will result in a Time limit exceeded. Time complexity will be O(n^2).
Efficient method
In this method, we avoid creating a 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 the 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 the frequency of k is k-1
Else frequency will be 2*n+1-k
Implementation:
CPP
#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);
}
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;
}
|
Java
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);
}
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" );
}
}
|
Python3
import math
def find( n, k):
if (n + 1 > = k):
return (k - 1 )
else :
return ( 2 * n + 1 - k)
n = 4
k = 7
freq = find(n, k)
if (freq < 0 ):
print ( " element not exist" )
else :
print ( " Frequency of " , k , " is " , freq )
|
C#
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);
}
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 );
}
}
|
PHP
<?php
function find( $n , $k )
{
if ( $n + 1 >= $k )
return ( $k - 1);
else
return (2 * $n + 1 - $k );
}
$n = 4;
$k = 7;
$freq = find( $n , $k );
if ( $freq < 0)
echo " element not exist \n " ;
else
echo " Frequency of " , $k
, " is " , $freq , "\n" ;
?>
|
Javascript
<script>
function find(n, k)
{
if (n + 1 >= k)
return (k - 1);
else
return (2 * n + 1 - k);
}
var n = 4, k = 7;
var freq = find(n, k);
if (freq < 0)
document.write( " element not exist <br>" );
else
document.write( " Frequency of " + k
+ " is " + freq + "<br>" );
</script>
|
Output
Frequency of 7 is 2
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
01 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...