You are given an integer ‘N’ denoting the size of a 'N' * 'N' square matrix 'mat', and an integer ‘K’.
The elements of matrix ‘mat’, holds a special property i.e. 'mat[i][j]' = i * j .Where 0 < i,j <= N. Your task is to return the occurrence of integer ‘K’ in the matrix ‘mat’.
Consider 1 based indexing.
Note:You are not given the actual matrix. Only the size of the matrix 'mat' is given.
Example:
For ‘N’ = 5 and ‘K’ = 5 :
The count of cells in the 5 * 5 matrices that contain the number ‘5’ will be 2.
{ ( 1, 5 ), ( 5, 1 ) }
The first line contains a single integer ‘T’ denoting the number of test cases to be run. Then the test cases follow.
The first line of each test case contains an integer 'N' denoting the number of rows and columns.
The next line of each test case contains an integer ‘K’.
Output Format :
For each test case, return an integer denoting the number of occurrences of ‘K'in the matrix ‘mat’.
Note :
You are not required to print anything, it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 10^4
1 <= K <= 10^9
Time Limit: 1 sec.
2
10
5
8
16
2
3
For N = 10 and K = 5, the required number of cells will be 2 {(1, 5), (5, 1)}.
For N = 8 and K = 16, the required number of cells will be 3 {(2, 8), (8, 2), (4, 4)}.
2
10
2
2
1
2
1
Element ‘K’ can be in any cell starting from (1, 1) to (N, N). Can you think of a solution now?
In this approach, we will check every element and count the occurrences of ‘K’.
Algorithm :
O(‘N’^2), where ‘N’ is the number of rows and columns.
As we are traversing every element in the 2-D matrix, hence, time complexity will be O(N^2).
O(1),
Constant extra space is used.