Program to find the number of persons wearing white hat
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
#include<bits/stdc++.h>
using namespace std;
int numOfWhiteHats( int arr[], int n)
{
int freq[n+1];
memset (freq, 0, sizeof (freq));
for ( int i=0; i<n; i++)
{
if (arr[i] >= n)
return -1;
freq[arr[i]]++;
}
int diffFreq = 0;
for ( int i = n-1; i >= 0; i--)
if (freq[i])
diffFreq++;
if (diffFreq == 1 && freq[n-1] == n)
return n;
if (diffFreq == 1 && freq[0] == n)
return 0;
if (diffFreq != 2)
return -1;
int k;
for (k = n-1; k >= 1; k--)
if (freq[k])
break ;
if (freq[k-1] == k && freq[k] + k == n)
return freq[k-1];
else
return -1;
}
int main()
{
int arr[] = {2, 2, 2, 3, 3};
int n = sizeof (arr)/ sizeof (arr[0]);
cout << numOfWhiteHats(arr, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG {
static int numOfWhiteHats( int arr[], int n)
{
int freq[] = new int [n + 1 ];
Arrays.fill(freq, 0 );
for ( int i = 0 ; i < n; i++) {
if (arr[i] >= n)
return - 1 ;
freq[arr[i]]++;
}
int diffFreq = 0 ;
for ( int i = n - 1 ; i >= 0 ; i--)
if (freq[i] > 0 )
diffFreq++;
if (diffFreq == 1 && freq[n - 1 ] == n)
return n;
if (diffFreq == 1 && freq[ 0 ] == n)
return 0 ;
if (diffFreq != 2 )
return - 1 ;
int k;
for (k = n - 1 ; k >= 1 ; k--)
if (freq[k] > 0 )
break ;
if (freq[k - 1 ] == k && freq[k] + k == n)
return freq[k - 1 ];
else
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = { 2 , 2 , 2 , 3 , 3 };
int n = arr.length;
System.out.print(numOfWhiteHats(arr, n));
}
}
|
Python3
def numOfWhiteHats(arr, n):
freq = [ 0 for i in range (n + 1 + 1 )]
for i in range (n):
if (arr[i] > = n):
return - 1
freq[arr[i]] + = 1
diffFreq = 0
for i in range (n - 1 , - 1 , - 1 ):
if (freq[i]):
diffFreq + = 1
if (diffFreq = = 1 and freq[n - 1 ] = = n):
return n
if (diffFreq = = 1 and freq[ 0 ] = = n):
return 0
if (diffFreq ! = 2 ):
return - 1
for k in range (n - 1 , 0 , - 1 ):
if (freq[k]):
break
if (freq[k - 1 ] = = k and freq[k] + k = = n):
return freq[k - 1 ]
else :
return - 1
arr = [ 2 , 2 , 2 , 3 , 3 ]
n = len (arr)
print (numOfWhiteHats(arr, n))
|
C#
using System;
class GFG {
static int numOfWhiteHats( int []arr, int n)
{
int []freq = new int [n + 1];
for ( int i = 0; i < n; i++) {
if (arr[i] >= n)
return -1;
freq[arr[i]]++;
}
int diffFreq = 0;
for ( int i = n - 1; i >= 0; i--)
if (freq[i] > 0)
diffFreq++;
if (diffFreq == 1 && freq[n - 1] == n)
return n;
if (diffFreq == 1 && freq[0] == n)
return 0;
if (diffFreq != 2)
return -1;
int k;
for (k = n - 1; k >= 1; k--)
if (freq[k] > 0)
break ;
if (freq[k - 1] == k && freq[k] + k == n)
return freq[k - 1];
else
return -1;
}
public static void Main()
{
int []arr = {2, 2, 2, 3, 3};
int n = arr.Length;
Console.WriteLine(numOfWhiteHats(arr, n));
}
}
|
Javascript
<script>
function numOfWhiteHats(arr, n)
{
var freq = Array(n + 1).fill(0);
for (i = 0; i < n; i++)
{
if (arr[i] >= n)
return -1;
freq[arr[i]]++;
}
var diffFreq = 0;
for (i = n - 1; i >= 0; i--)
if (freq[i] > 0)
diffFreq++;
if (diffFreq == 1 && freq[n - 1] == n)
return n;
if (diffFreq == 1 && freq[0] == n)
return 0;
if (diffFreq != 2)
return -1;
var k;
for (k = n - 1; k >= 1; k--)
if (freq[k] > 0)
break ;
if (freq[k - 1] == k && freq[k] + k == n)
return freq[k - 1];
else
return -1;
}
var arr = [ 2, 2, 2, 3, 3 ];
var n = arr.length;
document.write(numOfWhiteHats(arr, n));
</script>
|
Time Complexity: O(n)
Space Complexity: O(n) (we need to create a freq[] array of size n+1)
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...