4






Given an array of ints length 3, figure out which is larger between the first and last elements in the array, and set all the other elements to be that value. Return the changed array. 
maxEnd3({1, 2, 3}) → {3, 3, 3}
maxEnd3({11, 5, 9}) → {11, 11, 11}
maxEnd3({2, 11, 3}) → {3, 3, 3}

Solution:

public int[] maxEnd3(int[] nums) {
  int max = Math.max(nums[0], nums[2]);
  nums[0] = max;
  nums[1] = max;
  nums[2] = max;
  return nums;

  // Solution notes: you could write if-logic to figure out
  // which element is the biggest, but here we use Math.max()
  // to solve that part nicely.
}

OR


public int[] maxEnd3(int[] nums) {
  int[] maxVal=new int[3];
  maxVal[0]=nums[0];
  if(nums[2] >= maxVal[0])
  maxVal[0] = nums[2];
  maxVal[1] = maxVal[0];
  maxVal[2] = maxVal[0];
  return maxVal;
}

Post a Comment

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Training in Chennai Core Java 8 Training in Chennai Core Java 8 Training in Chennai JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete
  2. Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

    Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

    ReplyDelete
  3. Many specialized SEO tools can help you determine the popularity and the competitiveness of your possible keywords and can help improve your search engine ranking particularly in Google.Niche Blog Comments Available

    ReplyDelete
  4. Your work is very good and I appreciate you and hopping for some more informative posts. Thank you for sharing great information to us. SEO Services

    ReplyDelete

 
Top