Grouping Countries | CPP 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 program to test above function
#include <iostream> NEW_LINE using namespace std ; void countCountries ( int ans [ ] , int N ) { int total_countries = 0 , i = 0 ; bool invalid = false ; while ( i < N ) { int curr_size = ans [ i ] ; int num = ans [ i ] ; while ( num > 0 ) { if ( ans [ i ] != curr_size ) { cout << " Invalid ▁ Answer STRNEWLINE " ; return ; } else num -- ; i ++ ; } total_countries ++ ; } cout << " There ▁ are ▁ " << total_countries << " ▁ distinct ▁ companies ▁ in ▁ the ▁ group . STRNEWLINE " ; } int main ( ) { int ans [ ] = { 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 } ; int n = sizeof ( ans ) / sizeof ( ans [ 0 ] ) ; countCountries ( ans , n ) ; return 0 ; }