Skip to content

Latest commit

 

History

History
 
 

Matrix

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Matrix Problems Approach techniques

  Search in row and column sorted in O(log (M*N))
    Use Binary Search in Row and column simultaneously
    
     while i < m and n >= 0:
        element = top right corner
        if equal : return true
        else if (element < target): i++
        else n--
             
  Median in row and col sorted: O(32*r*log(c))
   Finding median and Kth smallest in matrix multiplication
    1. First find the min and max element in single traversal
    2. for r*c matrix order the median is (r*c+1)/2 if it is arranged in sorted order, so the desired place is (r*c+1)/2
    3. while min < max:
          place =0;    // for counting 
        mid=(min+max)/2;
        Count the number of elements smaller than mid by using upperbound which takes log(c) time for each row
         count+= upper_bound(matrix[i].begin(),matrix[i].end(), mid) -matrix[i].begin()
       if place < desired: min = mid + 1;
       else max = mid
       
       
   Method 2:
         1   2   3
      1  1   2   3
      2  2   4   6
      3  3   6   9
      
        1 2 2 3 3 4 6 6 9
        for i=1 to m: count+=min(mid/i,n)
      
   Rectangle with corner as 1
   Choose two rows with brute force method and traverse column
   now check their corresponding matrix[row1][column] and matrxi[row2][column] is 1
   
  Filp columns for max no.of equal rows
  	use map for storing freq
  	eg: 1 0 0 0 0
  	    0 1 1 1 1
  	    0 1 1 1 1
  	    if after fliping k columns the rows values r 0's and 1's means
  	    1's - for row x we need to XOR with its compliment
  	    0's - we need to xor with same