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 that number of people available then the answer is Invalid. Below is the implementation of idea :

## C++

`// CPP program to count no of distinct ` `// countries from a given group of people ` `#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]; ` ` ` ` ` `// Answer is valid if adjacent sitting ` ` ` `// num people give same answer ` ` ` `int` `num = ans[i]; ` ` ` `while` `(num > 0) { ` ` ` ` ` `// someone gives different answer ` ` ` `if` `(ans[i] != curr_size) { ` ` ` `cout << ` `"Invalid Answer\n"` `; ` ` ` `return` `; ` ` ` `} ` ` ` `else` ` ` `num--; ` ` ` ` ` `// check next person ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// one valid country group has ` ` ` `// been found ` ` ` `total_countries++; ` ` ` `} ` ` ` ` ` `cout << ` `"There are "` `<< total_countries ` ` ` `<< ` `" distinct companies in the group.\n"` `; ` `} ` ` ` `// driver program to test above function ` `int` `main() ` `{ ` ` ` `int` `ans[] = { 1, 1, 2, 2, 4, 4, 4, 4 }; ` ` ` `int` `n = ` `sizeof` `(ans) / ` `sizeof` `(ans[0]); ` ` ` `countCountries(ans, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count no of distinct ` `// countries from a given group of people ` ` ` `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]; ` ` ` ` ` `// Answer is valid if adjacent sitting ` ` ` `// num people give same answer ` ` ` `int` `num = ans[i]; ` ` ` `while` `(num > ` `0` `) { ` ` ` ` ` `// someone gives different answer ` ` ` `if` `(ans[i] != curr_size) { ` ` ` `System.out.print( ` `"Invalid Answer\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `else` ` ` `num--; ` ` ` ` ` `// check next person ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// one valid country group has ` ` ` `// been found ` ` ` `total_countries++; ` ` ` `} ` ` ` ` ` `System.out.print( ` `"There are "` `+ total_countries + ` ` ` `" distinct companies in the group.\n"` `); ` ` ` `} ` ` ` ` ` `// driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `ans[] = { ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `4` `, ` `4` `, ` `4` `, ` `4` `}; ` ` ` `int` `n = ` `8` `; ` ` ` `countCountries(ans, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count no of distinct ` `# countries from a given group of people ` ` ` `def` `countCountries(ans, N): ` ` ` `total_countries ` `=` `0` ` ` `i ` `=` `0` ` ` `invalid ` `=` `0` ` ` ` ` `while` `(i < N) : ` ` ` `curr_size ` `=` `ans[i] ` ` ` ` ` `# Answer is valid if adjacent sitting ` ` ` `# num people give same answer ` ` ` `num ` `=` `ans[i] ` ` ` `while` `(num > ` `0` `) : ` ` ` ` ` `# someone gives different answer ` ` ` `if` `(ans[i] !` `=` `curr_size) : ` ` ` `print` `(` `"Invalid Answer"` `) ` ` ` `return` `; ` ` ` `else` `: ` ` ` `num ` `=` `num ` `-` `1` ` ` ` ` `# check next person ` ` ` `i ` `=` `i ` `+` `1` ` ` ` ` `# one valid country group has ` ` ` `# been found ` ` ` `total_countries ` `=` `total_countries ` `+` `1` `; ` ` ` ` ` `print` `(` `"There are "` `, total_countries, ` ` ` `" distinct companies in the group."` `) ` ` ` `# Driven code ` `ans ` `=` `[ ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `4` `, ` `4` `, ` `4` `, ` `4` `]; ` `n ` `=` `len` `(ans); ` `countCountries(ans, n); ` ` ` `# This code is contributed by "rishabh_jain". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count no. of distinct ` `// countries from a given group of people ` `using` `System; ` ` ` `class` `Country { ` ` ` ` ` `// function to count no. of distinct ` ` ` `// countries from a given group of people ` ` ` `public` `static` `void` `countCountries(` `int` `[]ans, ` ` ` `int` `N) ` ` ` `{ ` ` ` `int` `total_countries = 0, i = 0; ` ` ` ` ` ` ` `while` `(i < N) { ` ` ` `int` `curr_size = ans[i]; ` ` ` ` ` `// Answer is valid if adjacent sitting ` ` ` `// num people give same answer ` ` ` `int` `num = ans[i]; ` ` ` `while` `(num > 0) { ` ` ` ` ` `// someone gives different answer ` ` ` `if` `(ans[i] != curr_size) { ` ` ` `Console.Write( ` `"Invalid Answer\n"` `); ` ` ` `return` `; ` ` ` `} ` ` ` `else` ` ` `num--; ` ` ` ` ` `// check next person ` ` ` `i++; ` ` ` `} ` ` ` ` ` `// one valid country group ` ` ` `// has been found ` ` ` `total_countries++; ` ` ` `} ` ` ` ` ` `Console.Write(` `"There are "` `+ total_countries + ` ` ` `" distinct companies in the group.\n"` `); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]ans = { 1, 1, 2, 2, 4, 4, 4, 4 }; ` ` ` `int` `n = 8; ` ` ` `countCountries(ans, n); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal ` |

*chevron_right*

*filter_none*

## PHP

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

*chevron_right*

*filter_none*

Output:

There are 4 distinct companies in the group.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Farthest index that can be reached from the Kth index of given array by given operations
- Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not
- Smallest pair of indices with product of subarray co-prime with product of the subarray on the left or right
- Print all strings from given array that can be typed using keys from a single row of a QWERTY keyboard
- Count binary strings of length same as given string after removal of substrings "01" and "00" that consists of at least one '1'
- Longest subarray in which all elements are a factor of K
- Lexicographically largest string possible consisting of at most K consecutive similar characters
- Append digits to the end of dupicate strings to make all strings in an array unique
- Count array elements whose product of digits is a Composite Number
- Count pairs from a given range whose sum is a Prime Number in that range
- Lexicographically smallest permutation of a string that can be reduced to length K by removing K-length prefixes from palindromic substrings of length 2K
- Minimum cost of flipping characters required to convert Binary String to 0s only
- Length of smallest meeting that can be attended
- Minimum distance to visit given K points on X-axis after starting from the origin

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.