0

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?

1
  • If you know how many elements are 1 or 2, then you could subtract those numbers from 100. Otherwise, you have to check every element. Commented Feb 10, 2016 at 0:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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".
1

No, if you want an exact count you have to at least look at your array. To look at it you need to read each value.

Also having 10x10 array efficiency does not really matter

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.