**New course!**Every coder should learn

**Generative AI!**

0

# Hi coders...M stuck here.. I want this code to return 1 if an element in the array has exactly one non zero neighbor. Like arr1

public class Program { public static int erachek(int [] arr) { for(int i=0;i<arr.length;i++){ if (arr[i]>0){ return 1; } } return 0; } public static void main(String [] args){ int [] arr1={0,3,2}; int [] arr2={7,6,9}; System.out.println (erachek (arr1)); System.out.println (erachek (arr2)); } }

4 Answers

+ 5

But condition is true for arr2 too as the first and the last elements for example have exactly 1 non zero neighbor.

+ 5

Then your question is wrong. It should be: the method returns 1 if there is no element in the array with two nonzero neighbors. Otherwise 0.
And what should it return if both neighbors are zeros.

0

in arr2 (6)has more than one non zero neighbor

0

therefore it shud return 0