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)); } }

8th Apr 2017, 6:08 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
4 odpowiedzi
+ 5
But condition is true for arr2 too as the first and the last elements for example have exactly 1 non zero neighbor.
8th Apr 2017, 6:20 AM
Tamás Barta
Tamás Barta - avatar
+ 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.
8th Apr 2017, 6:25 AM
Tamás Barta
Tamás Barta - avatar
0
in arr2 (6)has more than one non zero neighbor
8th Apr 2017, 6:21 AM
Ricardo Chitagu
Ricardo Chitagu - avatar
0
therefore it shud return 0
8th Apr 2017, 6:21 AM
Ricardo Chitagu
Ricardo Chitagu - avatar