• + 0 comments

    MY SOLUTION IN PHP

      $totalrow = count($grid);
        
        $columnArr = array();
        $allIndex = array();
    
        for ($i=0; $i <$totalrow ; $i++) { 
             $arr = str_split($grid[$i]);
             $columnArr[]  = $arr;
        }
        $columnLength = count($columnArr[0]);
    
    
        for ($i=1; $i < $totalrow-1; $i++) { 
    
            for ($j=1; $j <$columnLength-1 ; $j++) { 
                $rowis = $i+1;
                $columnIs = $j+1;
                
                $down = $totalrow-$rowis;
                $up = $totalrow - ($down +1);
                $right = $columnLength - ($columnIs);
                $left = $columnLength - ($right+1);
                $min = min($down,$up,$right,$left);
                
                
                for ($k=$min; $k >0 ; $k--) { 
                    
                     $totalValue = 0;
                     $flag = 0;
                     $indexArr = array();
                     $innerArr = array();
                     for ($l=0; $l <$k; $l++) { 
                         
                         
                          $val = $l+1;
                          $newupRow = $rowis -$val-1;
                          $newDownRow = $rowis+$val-1;
                         
                          $columnUpValue = $columnArr[$newupRow][$columnIs-1];
                         
    
                          $columnDownvalue = $columnArr[$newDownRow][$columnIs-1];
                         
    
                         
                          $colLeft = ($columnIs-1)-($l+1);
                           $columnLeftValue = $columnArr[$rowis-1][$colLeft];
                          
    
                           $colRight = ($columnIs-1)+($l+1);
                            $columnRightValue = $columnArr[$rowis-1][$colRight];
                         
    
                           if($columnUpValue === 'G' && $columnDownvalue === 'G' && $columnLeftValue === 'G' && $columnRightValue === 'G' && $columnArr[$i][$j] === 'G' && $l === 0 )
                            {
                                $flag = 1;
                                $totalValue+= 4;
                           
                             $innerArr[] =$newupRow.','.$columnIs-1;
                             $innerArr[] =$newDownRow.','.$columnIs-1;
                             $innerArr[] =($rowis-1).','.$colLeft;
                             $innerArr[] =($rowis-1).','.$colRight;
                             $innerArr[] =$i.','.$j;
    
                            }
                            else if($columnUpValue === 'G' && $columnDownvalue === 'G' && $columnLeftValue === 'G' && $columnRightValue === 'G'  && $columnArr[$i][$j] === 'G' && $flag === 1 )
                            {
                                $flag = 1;
                                $totalValue+= 4;
                            
                            $innerArr[] =$newupRow.','.$columnIs-1;
                             $innerArr[] =$newDownRow.','.$columnIs-1;
                             $innerArr[] =($rowis-1).','.$colLeft;
                             $innerArr[] =($rowis-1).','.$colRight;
                           
                           
                            }
                             else
                            {
                                break;
                            }
    
                           if(count($innerArr)>0)
                           {
                                $indexArr[] = $innerArr;
    
                           } 
    
    
                     }
                    
                    
                       if(count($indexArr)>0)
                           {
                                $allIndex[] = $indexArr;
    
                           } 
                      
                    
    
    
                }
                
            }
    
            
        }
    
    
     
        
    
    $result = array();
    foreach ($allIndex as $outer) {
        foreach ($outer as $inner) {
            $result[] = $inner;
        }
    }
        
    
        $unique = [];
    foreach ($result as $sub) {
        $temp = $sub;
        sort($temp);                
        $key = json_encode($temp);  
        $unique[$key] = $sub;    
    }
    
    
    $result = array_values($unique);
    
    
    $resultLen = count($result);
    
    $ans = 0;
    if ($resultLen == 1) {
        $ans = count($result[0]);
    }
    else
    {
        $productArray = [];
    for ($i=0; $i < $resultLen-1; $i++) { 
        
        $first = $result[$i];
        $firstLen = count($first);
        
         for ($j=$i+1; $j < $resultLen ; $j++) { 
            $second = $result[$j];
            $secondLen = count($second);
    
            $common = array_intersect($first, $second);
    
            if (!empty($common)) {
                $productArray[] = max($firstLen,$secondLen);
              
            } else {
                $secondLen = count($second);
                $productArray[] = $firstLen*$secondLen;
               
            }
    
         }
    }
    
    $ans = count($productArray) > 0 ? max($productArray) : 1;
    
    }
    
    return $ans;