Company: Navi Technologies
Difficulty: medium
Count Grid Coloring Patterns Problem Description An automated painting system needs a program that can paint an n x 3 grid using three distinct colors: red (R), green (G), and blue (B). The coloring must adhere to a specific rule: no row or column should contain cells that are all the same color. For example, a row like [R, R, R] or a column where all three cells are [G, G, G] (vertically) would be invalid. However, a row like [R, G, R] or [R, R, G] is valid, as not all cells in that row are the same color. Similarly for columns. Given an integer n , representing the number of rows, determine the total number of valid patterns that can be painted on an n x 3 grid. Since the number of patterns can be very large, return the result modulo (10^9 + 7) . For instance, a 4 x 3 grid has 296490 valid patterns. Complete the countPatterns function in the editor below. Parameter: int n : The number of rows in the n x 3 grid. Returns: int : The number of ways in which the grid can be colored, calcu