There are N persons in a room, each of them wearing a hat which is either black or white. Every person counts the number of other persons wearing the white hat. Given the number of counts of each person, the task is to find the number of persons wearing white hats, or print -1 if the given counts don’t correspond to a valid situation.
Examples:
Input : arr[] = {2, 1, 1}. Output : 2 First person sees two white hats. Second and third persons see one white hat. The first person must be wearing a black hat and other two must be wearing a white hat. Input : arr[] = {2, 2, 2} Output : 3 All are wearing white hats. Input : arr[] = {10, 10} Output : -1 There are only two persons, count can't be 10.
There are only two kinds of persons. If each person counts correctly (valid situation), then the count value of each person wearing white hat is same. And also, the count value of each person wearing black hat is same. So there will be only one or two types of value in the array.
Let the number of white hats be i, 0 <= i <= N-1.
Now observe for each person wearing the white hat, the count value will be i – 1. So there will be i persons whose count will be i-1.
Also the number of persons wearing the black hats will be, N – i and their given count value will be i.
An interesting case is with zero white hats. If all values are 0, then everybody is wearing a black hat. Otherwise there can be at most one zero for the case when there is single person wearing a white hat. In case of one zero, all other entries must be 1.
Algorithm for solving this problem:
- Count the frequency of each element of the array.
- Since there are one or two types, say x and y.
- If the number of x’s equal to x + 1 and number of y’s equal to n – y. The Number of hats equal to y or x + 1.
- Otherwise print -1.
Explained example:
Suppose, N = 5, the number of white hats can be range from 0 to 4. For white hats = 1, array will be {0, 1, 1, 1, 1}. Number of 0's = 0 + 1 = 1. Number of 1's = 5 - 1 = 4. For white hats = 2, array will be {1, 1, 2, 2, 2}. Number of 1's = 1 + 1 = 2. Number of 2's = 5 - 3 = 2. For white hats = 3, array will be {2, 2, 2, 3, 3}. Number of 2's = 2 + 1 = 3. Number of 3's = 5 - 3 = 2. For white hats = 5, array will be {4, 4, 4, 4, 4}. Number of 4's = 4 + 1 = 5. Number of 5's = 5 - 5 = 0.
Below is the implementation of this approach:
CPP
// C++ program to count number of white hats #include<bits/stdc++.h> using namespace std; // Given counts of White hats seen by n people, // return count of white hats. int numOfWhiteHats( int arr[], int n) { // Counting frequencies of all values in given // array int freq[n+1]; memset (freq, 0, sizeof (freq)); for ( int i=0; i<n; i++) { // Count of White hats cannot be more than // n for n persons. if (arr[i] >= n) return -1; freq[arr[i]]++; } // Counting number of different frequencies int diffFreq = 0; for ( int i = n-1; i >= 0; i--) if (freq[i]) diffFreq++; // Cases where all the persons wearing white hat. if (diffFreq == 1 && freq[n-1] == n) return n; // Case where no one wearing white hat. if (diffFreq == 1 && freq[0] == n) return 0; // Else : number of distinct frequency must be 2. if (diffFreq != 2) return -1; // Finding the last frequency with non zero value. // Note that we traverse from right side. int k; for (k = n-1; k >= 1; k--) if (freq[k]) break ; // Checking number of k's must be n - k. // And number of (k-1)'s must be k. if (freq[k-1] == k && freq[k] + k == n) return freq[k-1]; else return -1; } // Driver code int main() { int arr[] = {2, 2, 2, 3, 3}; int n = sizeof (arr)/ sizeof (arr[0]); cout << numOfWhiteHats(arr, n); return 0; } |
Java
// Java program to count number of white hats import java.util.Arrays; class GFG { // Given counts of White hats seen by n // people, return count of white hats. static int numOfWhiteHats( int arr[], int n) { // Counting frequencies of all values // in given array int freq[] = new int [n + 1 ]; Arrays.fill(freq, 0 ); for ( int i = 0 ; i < n; i++) { // Count of White hats cannot be // more than n for n persons. if (arr[i] >= n) return - 1 ; freq[arr[i]]++; } // Counting number of different // frequencies int diffFreq = 0 ; for ( int i = n - 1 ; i >= 0 ; i--) if (freq[i] > 0 ) diffFreq++; // Cases where all the persons wearing // white hat. if (diffFreq == 1 && freq[n - 1 ] == n) return n; // Case where no one wearing white hat. if (diffFreq == 1 && freq[ 0 ] == n) return 0 ; // Else : number of distinct frequency // must be 2. if (diffFreq != 2 ) return - 1 ; // Finding the last frequency with non // zero value. // Note that we traverse from right side. int k; for (k = n - 1 ; k >= 1 ; k--) if (freq[k] > 0 ) break ; // Checking number of k's must be n - k. // And number of (k-1)'s must be k. if (freq[k - 1 ] == k && freq[k] + k == n) return freq[k - 1 ]; else return - 1 ; } // Driver code public static void main(String[] args) { int arr[] = { 2 , 2 , 2 , 3 , 3 }; int n = arr.length; System.out.print(numOfWhiteHats(arr, n)); } } // This code is contributed by Anant Agarwal. |
Python3
# python program to count # number of white hats def numOfWhiteHats(arr, n): # Counting frequencies of # all values in given # array freq = [ 0 for i in range (n + 1 + 1 )] for i in range (n): # Count of White hats # cannot be more than # n for n persons. if (arr[i] > = n): return - 1 freq[arr[i]] + = 1 # Counting number of # different frequencies diffFreq = 0 for i in range (n - 1 , - 1 , - 1 ): if (freq[i]): diffFreq + = 1 # Cases where all the # persons wearing white hat. if (diffFreq = = 1 and freq[n - 1 ] = = n): return n # Case where no one # wearing white hat. if (diffFreq = = 1 and freq[ 0 ] = = n): return 0 # Else : number of distinct # frequency must be 2. if (diffFreq ! = 2 ): return - 1 # Finding the last frequency # with non zero value. # Note that we traverse # from right side. for k in range (n - 1 , 0 , - 1 ): if (freq[k]): break # Checking number of k's # must be n - k. # And number of (k-1)'s # must be k. if (freq[k - 1 ] = = k and freq[k] + k = = n): return freq[k - 1 ] else : return - 1 # Driver code arr = [ 2 , 2 , 2 , 3 , 3 ] n = len (arr) print (numOfWhiteHats(arr, n)) # This code is contributed # by Anant Agarwal. |
C#
// C# program to count number of white hats using System; class GFG { // Given counts of White hats seen by n // people, return count of white hats. static int numOfWhiteHats( int []arr, int n) { // Counting frequencies of all values // in given array int []freq = new int [n + 1]; //Arrays.fill(freq, 0); for ( int i = 0; i < n; i++) { // Count of White hats cannot be // more than n for n persons. if (arr[i] >= n) return -1; freq[arr[i]]++; } // Counting number of different // frequencies int diffFreq = 0; for ( int i = n - 1; i >= 0; i--) if (freq[i] > 0) diffFreq++; // Cases where all the persons wearing // white hat. if (diffFreq == 1 && freq[n - 1] == n) return n; // Case where no one wearing white hat. if (diffFreq == 1 && freq[0] == n) return 0; // Else : number of distinct frequency // must be 2. if (diffFreq != 2) return -1; // Finding the last frequency with non // zero value. // Note that we traverse from right side. int k; for (k = n - 1; k >= 1; k--) if (freq[k] > 0) break ; // Checking number of k's must be n - k. // And number of (k-1)'s must be k. if (freq[k - 1] == k && freq[k] + k == n) return freq[k - 1]; else return -1; } // Driver code public static void Main() { int []arr = {2, 2, 2, 3, 3}; int n = arr.Length; Console.WriteLine(numOfWhiteHats(arr, n)); } } // This code is contributed by vt_m. |
Javascript
<script> // javascript program to count number of white hats // Given counts of White hats seen by n // people, return count of white hats. function numOfWhiteHats(arr, n) { // Counting frequencies of all values // in given array var freq = Array(n + 1).fill(0); for (i = 0; i < n; i++) { // Count of White hats cannot be // more than n for n persons. if (arr[i] >= n) return -1; freq[arr[i]]++; } // Counting number of different // frequencies var diffFreq = 0; for (i = n - 1; i >= 0; i--) if (freq[i] > 0) diffFreq++; // Cases where all the persons wearing // white hat. if (diffFreq == 1 && freq[n - 1] == n) return n; // Case where no one wearing white hat. if (diffFreq == 1 && freq[0] == n) return 0; // Else : number of distinct frequency // must be 2. if (diffFreq != 2) return -1; // Finding the last frequency with non // zero value. // Note that we traverse from right side. var k; for (k = n - 1; k >= 1; k--) if (freq[k] > 0) break ; // Checking number of k's must be n - k. // And number of (k-1)'s must be k. if (freq[k - 1] == k && freq[k] + k == n) return freq[k - 1]; else return -1; } // Driver code var arr = [ 2, 2, 2, 3, 3 ]; var n = arr.length; document.write(numOfWhiteHats(arr, n)); // This code is contributed by Rajput-Ji. </script> |
3
Time Complexity: O(n)
Space Complexity: O(n) (we need to create a freq[] array of size n+1)
If you like neveropen and would like to contribute, you can also write an article using write.neveropen.co.uk or mail your article to review-team@neveropen.co.uk. See your article appearing on the neveropen main page and help other Geeks.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!