Solution is 1,here is the approach


dwarfs will follow the strategy, the last dwarf will count and shout the color of hat which is present “even” number of times.So maybe he will get killed maybe not but,atleast other dwarf will get to know that which colour hat is present even number of times and which hat is present odd number of times or in case of even “N” both will be even
so n-1(th) person will count hats on head of (n-2) persons and answer accordingly.
Hope it helps.


what if both black and white number of hats are even
then he will not able to tell what the last dwarf was trying to tell


Both hats are even for the last guy,for 2nd last guy one colour hat is even and other is odd… think about it.
if both are even last guy can scream any colour.