Grouping Countries | PHP program to count no of distinct countries from a given group of people ; Answer is valid if adjacent sitting num people give same answer ; someone gives different answer ; check next person ; one valid country group has been found ; Driver Code
< ? php function countCountries ( $ ans , $ N ) { $ total_countries = 0 ; $ i = 0 ; $ invalid = false ; while ( $ i < $ N ) { $ curr_size = $ ans [ $ i ] ; $ num = $ ans [ $ i ] ; while ( $ num > 0 ) { if ( $ ans [ $ i ] != $ curr_size ) { echo " Invalid ▁ Answer STRNEWLINE " ; return ; } else $ num -- ; $ i ++ ; } $ total_countries ++ ; } echo " There ▁ are ▁ " , $ total_countries , " ▁ distinct ▁ companies ▁ in ▁ the ▁ group . STRNEWLINE " ; } $ ans = array ( 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ) ; $ n = sizeof ( $ ans ) ; countCountries ( $ ans , $ n ) ; ? >