• + 46 comments

    For those, who, like me, struggle to understand the algorithm even after googling one. It took me time, imagination and a couple of colored pens, but I figured out the pattern. Look at this image.

    https://drive.google.com/uc?export=view&id=1t_7fU2LD06qfyIOiFTJ51X8npx0qmcca

    The pattern is that concrete elements can only be 'flipped' to certain positions. Looking at coordinates, you can always say what other elements can be placed instead of this exact element.

    So what you need to do is:

    1. Pick an element from the top left quarter.
    2. Find its 'beyond the mirror' mates.
    3. Choose the maximum element.

    And sum them up. Voila!

    The general rule:

    For every m[i][j] from the top left quarter:
    
    m[i][j]  m[i][(2n-1)-j]  m[(2n-1)-i][j]  m[(2n-1)-i][(2n-1)-j]
    
    • + 0 comments

      Oh my god. I looked at that image and I immediately understood the pattern. Thank you so much.

    • + 0 comments

      Thanks so much this was really helpful

    • + 0 comments

      Thanx for sharing, really helpful

    • + 0 comments

      Thank you so much, I couldn't figure out how to do it until I saw your pattern! I'd like to know how you came up with this solution!

    • + 1 comment

      will this pattern work for bigger number of n for example if n=3, the square matrix will be 6x6, how will you make this exact pattern now?

      • + 1 comment

        This pattern works for every positive n because it's basically about rotating a square array and getting "upside-down", "right-to-left" and "reversed diagonal" views. I suggest you take a sheet of paper and imagine how the similar matrix of letters 3x3 can be flipped in each of 3 directions. This schema can then be applied to the initial 6x6 matrix.

        • + 0 comments

          Yes! This finally helped me get to a solution! Thank you!

          Very verbose, to facilitate understanding and debugging:

          def flippingMatrix(matrix)
              groups = []
          
              low_index = 0
              high_index = matrix.first.size - 1
          
              while low_index < high_index do
                  x = 0
                  y = matrix.first.size - 1
                  
                  while x < y do
                      groups << [
                          matrix[low_index][x],
                          matrix[low_index][y],
                          matrix[high_index][x],
                          matrix[high_index][y]
                      ]
          
                      x += 1
                      y -= 1
                  end
          
                  low_index += 1
                  high_index -= 1
              end
          
              sum = 0
          
              groups.each do |group|
                  sum += group.max
              end
          
              sum
          end
          
    • + 0 comments

      This is Brilliant!

    • + 0 comments

      Thanks for sharing! The image really helped me to understand the solution.

    • + 0 comments

      Thanks to you, I got a lot of help. 감사합니다~

    • + 0 comments

      that diagram helped SO much, thank you for sharing!!

    • + 0 comments

      You are amazing!!! Russian people are the math masters!

    • + 0 comments

      This is awesome, the picture kill it. Thanks so much! Just wish one day I will be able to craft brilliant solutions like this!

    • + 0 comments

      great image, thanks.

    • + 0 comments

      Thats a pretty good clarification

    • + 0 comments

      Love the visual and your pseudocode... very helpful!

    • + 0 comments

      But what if you have a 50x50 matrix ? sure you still have 1 of 4 corners, but then you have to find the max 25x25 top left corner combination.

      I don't get it this challenge is so crazy....

    • + 0 comments

      Thank you for sharing! this is really helpful on improving my algorithm for this challenge

    • + 0 comments

      I didn't even understand the question until I saw your artwork, actually I still don't know the question, but now I know the answer... as indian jones said... it belongs in the louver!

    • + 0 comments

      OMG thank you so much for this. It gave me some clues, even though I still do not know how to solve it. I am struggling with this exercise A LOT!

    • + 0 comments

      Pictures are worth a thousand words. Thanks much for posting this!

    • + 0 comments

      That was really a very good explanation. Thank you so much

    • + 0 comments

      Thank you so much for clarify the question! I really didn't get what was the actually problem but with your image it became very very understandable.

    • + 0 comments

      Yes, I was actually struggling to understand, until i did some console.log() in an existing solution and came back to this diagram. Then it all make sense. Thanks for this.

      Im guess people will keep thanking you in 100years to come,LOL

    • + 0 comments

      Before seeing the image I actually forgot that with little effort you can get any point anywhere without disturbing the other points like rubix cube. So just check the currosponding points

    • + 0 comments

      Thank you so much!

    • + 0 comments

      Thank you for sharing this. Image was worth a 1000 words.

    • + 0 comments

      Please help hell_io for i:=0;i

    • + 0 comments

      Thanks for the image..

    • + 0 comments

      This helped a lot, THANKS for sharing

    • + 0 comments

      Bless you, this was super helpful and made the problem easier to understand.

    • + 0 comments

      Thank you so much for the clarity.

    • + 0 comments

      I came up with this solution based on your explanation.

      def flippingMatrix(matrix):

      l = len(matrix)-1
      num_groups = (len(matrix)**2)/4
      iterator = int(num_groups**(1/2))
      groups = []
      
      for i in range(iterator):
          for j in range(iterator):
              groups.append([matrix[i][j], matrix[i][l-j], matrix[l-i][j], matrix[l-i][l-j]])
      maxs = []
      for i in range(len(groups)):
          maxs.append(max(groups[i]))
      
      return sum(maxs)
      
    • + 0 comments

      very helpful!

    • + 0 comments

      Nice job,

      I apply your idea:

      1. Iterate over the elements from top left of the matrix.
      2. Create a function, that given a coordinate (i,j) and the lenght of the matrix (n), collect all the possibles coordinates to observe.
      3. Query the matrix on the coordinates in step #2 and collect the max
      4. The step #3 return an array of length n, apply a sum on that array, that is the solution.
      def collect_limits(i: int, j: int, n: int) -> list:
          out =  [ [i, j], [i, n-j-1], [n-i-1, j], [n-i-1, n-j-1] ]
          return out
      
    • + 0 comments

      Hey you - that is just great!!!!!!!!!! I am so very prod of you. Thank you very much.

    • + 0 comments

      Thank you so much for your help. I literally understood the problem immediatly just by looking the image, and reading the general rule made it more easier.

    • + 1 comment

      but when you find max value for a certain position and flip the necesary row/col this may alter another already sorted col/row... so its more complex to just find max values and add it to total. Look at this example: original matrix:

      [112 42 83 119 ]

      [56 125 56 49 ]

      [15 78 101 43 ]

      [62 98 114 108 ]

      max A: 119 reversed row: 0

      [119 83 42 112 ]

      [56 125 56 49 ]

      [15 78 101 43 ]

      [62 98 114 108 ]

      max B: 114 reversed row: 3 reversed col: 1

      [119 114 42 112 ]

      [56 78 56 49 ]

      [15 125 101 43 ]

      [108 83 98 62 ]

      max C: 56 Not necesary flip

      [119 114 42 112 ]

      [56 78 56 49 ]

      [15 125 101 43 ]

      [108 83 98 62 ]

      max D: 125 reversed col: 1 <<<--- HERE

      [119 83 42 112 ]

      [56 125 56 49 ]

      [15 78 101 43 ]

      [108 114 98 62 ]

      --final matrix--

      [119 83 42 112 ]

      [56 125 56 49 ]

      [15 78 101 43 ]

      As you can see, when i reversed column 1 to get value 125 on D, it alter B =114

      ee, when i reversed column 1 to get value 125 on D, it alter B =114

      • + 0 comments

        I think the designer of the problem, should answer this observation. Please do not say to solve it with below solution, since we all know this solution works. However, this solution, was not described the way the original question is described.

        Thanks

        sum_arr = []

        for row in range(n):
            for col in range(n):
                max_of = max([matrix[row][col],matrix[row][2*n-1-col],matrix[2*n-1-row][col],matrix[2*n-1-row][2*n-1-col]])
                print("Comparing max among these elements:")
                print([matrix[row][col],matrix[row][2*n-1-col],matrix[2*n-1-row][col],matrix[2*n-1-row][2*n-1-col]])
                sum_arr.append(max_of)
                print(row, col, max_of, sum_arr)
        return sum(sum_arr)
        
    • + 0 comments

      Thank you so much for your explanation

    • + 0 comments

      it is really helpful to visualize :)

    • + 0 comments

      Goodness. I was on the right track, just didnt connect the dots. Great explanation!

    • + 0 comments

      Thank you, this was really elegantly explained!

    • + 0 comments

      Thank you for the picture!

    • + 0 comments

      Thank you!!! This really made me understand the solution!!!

    • + 0 comments

      The image was really the key, ty!

    • + 0 comments

      Hi Hello_io, thanks for the explanation. much appreciated. My confusion is that, if we keep reversing entire rows and columns, do we ever end up at the best possible highest sum ever? thanks.!

    • + 0 comments

      Thank you. Really helpful explanation.