Monday 5 January 2015

Problem : sameFirstLast Element

 return true if the array is length 1 or more, and the first element and the last element are equal. 
  public boolean sameFirstLast(int[] nums)
       {
                if(nums.length>1)
                       return((nums[0]==nums[nums.length-1]));
                else if(nums.length==1)
                       return true;
                else
                       return false;
       }

Problem Name : commonEnd in java. return true if they have the same first element or they have the same last element

      
Solution:
Hint:  return true if they have the same first element or they have the same last element

public boolean commonEnd(int[] a, int[] b)
       {
                if(a.length>=1 && b.length >=1)
                       return ((a[0]==b[0])||(a[a.length-1]==b[b.length-1]));
                else  
                       return (false);
       }