2






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. 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
  2. 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