Skip to content
Farrukh Ahmad edited this page Dec 30, 2015 · 1 revision

Lets check whether given array satisfy following conditions

  1. First and Last element addition is less than 10
  2. second and last but one addition is less than 10

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

Clone this wiki locally