Grouping Countries
Last Updated :
21 Jun, 2022
People in a group, are sitting in a row numbered from 1 to n. Every has been asked the same question, “How many people of your country are there in the group?”
The answers provided by the people may be incorrect. People of the same country always sit together. If all answers are correct determine the number of distinct countries else print “Invalid Answer”.
Examples:
Input : ans[] = {1, 3, 2, 2}
Output : Invalid Answer
The second person says there are 3
people from his country however
the person sitting next to him says
there are 2 people. Hence this is
an invalid answer.
Input : ans[] = {1, 1, 2, 2, 4, 4,
4, 4}
Output : 4
There are 1 person each representing
two distinct countries. In the next
one there are two people and in the
fourth one there are 4 people from
the same country.
Source : ThoughtWorks Application Qualifier Test
This is a basic problem that can be solved in linear time. We will take a variable curr_size that tells us the size of the current country being considered. Whatever the size is, next ‘size’ number of people should give the same answer in order for a valid group to be formed. If anyone gives a different answer or there are less that number of people available then the answer is Invalid. Below is the implementation of idea :
C++
#include <iostream>
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\n" ;
return ;
}
else
num--;
i++;
}
total_countries++;
}
cout << "There are " << total_countries
<< " distinct companies in the group.\n" ;
}
int main()
{
int ans[] = { 1, 1, 2, 2, 4, 4, 4, 4 };
int n = sizeof (ans) / sizeof (ans[0]);
countCountries(ans, n);
return 0;
}
|
Java
class Country
{
public static void countCountries( int ans[],
int N)
{
int total_countries = 0 , i = 0 ;
boolean invalid = false ;
while (i < N) {
int curr_size = ans[i];
int num = ans[i];
while (num > 0 ) {
if (ans[i] != curr_size) {
System.out.print( "Invalid Answer\n" );
return ;
}
else
num--;
i++;
}
total_countries++;
}
System.out.print( "There are " + total_countries +
" distinct companies in the group.\n" );
}
public static void main(String[] args)
{
int ans[] = { 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 };
int n = 8 ;
countCountries(ans, n);
}
}
|
Python3
def countCountries(ans, N):
total_countries = 0
i = 0
invalid = 0
while (i < N) :
curr_size = ans[i]
num = ans[i]
while (num > 0 ) :
if (ans[i] ! = curr_size) :
print ( "Invalid Answer" )
return ;
else :
num = num - 1
i = i + 1
total_countries = total_countries + 1 ;
print ( "There are " , total_countries,
" distinct companies in the group." )
ans = [ 1 , 1 , 2 , 2 , 4 , 4 , 4 , 4 ];
n = len (ans);
countCountries(ans, n);
|
C#
using System;
class Country {
public static void countCountries( int []ans,
int N)
{
int total_countries = 0, i = 0;
while (i < N) {
int curr_size = ans[i];
int num = ans[i];
while (num > 0) {
if (ans[i] != curr_size) {
Console.Write( "Invalid Answer\n" );
return ;
}
else
num--;
i++;
}
total_countries++;
}
Console.Write( "There are " + total_countries +
" distinct companies in the group.\n" );
}
public static void Main()
{
int []ans = { 1, 1, 2, 2, 4, 4, 4, 4 };
int n = 8;
countCountries(ans, n);
}
}
|
PHP
<?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\n" ;
return ;
}
else
$num --;
$i ++;
}
$total_countries ++;
}
echo "There are " , $total_countries
, " distinct companies in the group.\n" ;
}
$ans = array (1, 1, 2, 2, 4, 4, 4, 4 );
$n = sizeof( $ans );
countCountries( $ans , $n );
?>
|
Javascript
<script>
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( "Invalid Answer\n" );
return ;
}
else
num--;
i++;
}
total_countries++;
}
document.write( "There are " + total_countries +
" distinct companies in the group.\n" );
}
let ans = [ 1, 1, 2, 2, 4, 4, 4, 4 ];
let n = 8;
countCountries(ans, n);
</script>
|
Output:
There are 4 distinct companies in the group.
Share your thoughts in the comments
Please Login to comment...