Grouping Countries | JavaScript 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
function countCountries ( ans , N ) { let total_countries = 0 , i = 0 ; let invalid = false ; while ( i < N ) { let curr_size = ans [ i ] ; let num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { document . write ( " " ) ; return ; } else num -- ; i ++ ; } total_countries ++ ; } document . write ( " " + total_countries + " " ) ; } let ans = [ 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ] ; let n = 8 ; countCountries ( ans , n ) ;