Lets check whether given array satisfy following conditions
like
[1, 2, 19, 4, 5] is Onion as 1+5 < 10 and 2+4 < 10
[1, 2, 19, 4, 10] is not Onion as 1+10 >= 10 2+4 < 10
public class Onion { public static void main(String[] args) { int a[] = {1, 3, 9, 8}; System.out.println(isOnion(a)); } static boolean isOnion(int a[]){ if(a.length == 0) return true; if(a.length == 1 && a[0] < 10) return true; for(int j=0, k=a.length-1; j<=k; j++,k--){ if(a[j] + a[k] > 10) return false; } return true; } }
There was an error while loading. Please reload this page.