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.
- Search in a sorted 2D matrix (Stored in row major order)
- Search in a row wise and column wise sorted matrix
- Kth smallest element in a row-wise and column-wise sorted 2D array | Set 1
- Count zeros in a row wise and column wise sorted matrix
- Find the element at R'th row and C'th column in given a 2D pattern
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix
- Leftmost Column with atleast one 1 in a row-wise sorted binary matrix | Set 2
- Count of array elements whose order of deletion precedes order of insertion
- Find row number of a binary matrix having maximum number of 1s
- Count positions such that all elements before it are greater
- How to validate an IP address using ReGex
- Find the row with maximum number of 1s
- Print number in ascending order which contains 1, 2 and 3 in their digits.
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Maximum sum of elements from each row in the matrix
- Find maximum element of each row in a matrix
- Count of elements of an array present in every row of NxM matrix
- Find the position of the given row in a 2-D array
- Find pair with maximum difference in any column of a Matrix
- Find next greater number with same set of digits
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.