I have a 10x10 2D array, each element is either a 0, 1, or 2. The task is to count how many total elements in the array are 0. Currently I'm looping through the array, and increment an int counter variable if the current element is 0. Is there a more efficient way to do this, instead of checking every element in the array?
2 Answers
How could there be a more efficient way?
Think about it, if you want to find out how many elements in a matrix are zero, then you have to look at each of them to decide their values. Computers can do quick calculations, but they can not do impossible things.
2 Comments
tarunbod
Right, that's what I was initially thinking. Of course some less knowledgable people tried to convince me otherwise, leaving me in a doubtful state which compelled me to write that question in the first place. Thanks.
Harald Nordgren
Do share, it's an interesting discussion. What were their explanations for how you would go about doing it? Or I was it just a vague sense of "there's some magical way it can be done faster".