Given a 2D array of size M x N. Calculate count of positions in 2D array where address as per row major order equals to address as per column major order.
Input : 3 5
Output : 3
Row major address is same as column major for following i, j
pairs (1, 1), (2, 3) & (3, 5)
Input : 4 4
Output : 4
Lets consider element with index i, j
Row major address = B + w * (N * (i-1) + j-1) Column major address = B + w * (M * (j-1) + i-1)
B : Base address of the array
w : Size of each element of the array
Equating both addresses, we get B + w * (N * (i-1) + j-1) = B + w * (M * (j-1) + i-1) N * (i-1) + j = M * (j-1) + i N*i - N + j = M*j - M + i M*j - j = N*i - N + M - i (M-1) * j = N*i - N + M - i j = (N*i - N + M - i)/(M-1) - (Eq. 1) Similarly i = (M*j - M + N - j)/(N-1) - (Eq. 2)
Now we have established a relation between i and j
Iterate for all possible i and find corresponding j If j comes out to be an integer in the range 1 to N, increment the counter.
Below is the implementation of above approach.
Time Complexity: O(M)
Complexity can be reduced to O(min(M, N)) by establishing relation of i in terms of j(Eq. 2) and iterating for all possible j in case N<M and by establishing relation j in terms of i(Eq. 1) and iterating for all possible i otherwise.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Performance analysis of Row major and Column major order of storing arrays in C
- Count positions in Binary Matrix having equal count of set bits in corresponding row and column
- Search in a sorted 2D matrix (Stored in row major order)
- Check if row-major order path of Matrix is palindrome or not
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Check if sums of i-th row and i-th column are same in matrix
- Count number of islands where every island is row-wise and column-wise separated
- Construct a Binary Matrix whose sum of each row and column is a Prime Number
- Count the number of pairs that have column sum greater than row sum
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Find the element at R'th row and C'th column in given a 2D pattern
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.