How do I find a maximum number of consecutive non-zero values in the array?

Your code passes all these tests, @GoToLoop:

  • final int[] values = {}; // empty array
  • final int[] values = {0}; // one zero value
  • final int[] values = {0, 0, 0}; // several zero values
  • final int[] values = {1}; // one non-zero value
  • final int[] values = {1, 1, 1}; // several non-zero values
  • final int[] values = {0, 1, 1}; // zero value first
  • final int[] values = {1, 1, 0}; // zero value last
  • final int[] values = {1, 1, 0, 1, 1, 1}; // longest non-zero sequence at end
  • final int[] values = {1, 1, 1, 0, 1, 1}; // longest non-zero sequence at beginning

Another remedy that does work is for @batmm to move the inner conditional block to a particular place, so that it executes even when the longest sequence of non-zero values occurs at the end of the array.

2 Likes