Java > Array-1 > maxEnd3 (CodingBat Recursive Solution)

Problem:

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) {
  if (nums[0] >= nums[nums.length-1]) {
  nums[0] = nums[0];
  nums[1] = nums[0];
  nums[2] = nums[0];
  }
  else if (nums[0] <= nums[nums.length-1]) {
  nums[0] = nums[nums.length-1]; 
  nums[1] = nums[nums.length-1]; 
  nums[2] = nums[nums.length-1];
  }
  return new int[] { nums[0],nums[1],nums[2]};
}

If you find this solution hard you might be looking for a normal and easy (non-recursive) solution here. 


15 comments :

  1. in line 2 declaring integer index looks odd and shouldbe removed from code i guess

    ReplyDelete
    Replies
    1. I agree, it's a code-typo :). Thanks for noticing, I will remove it.

      Delete
  2. Do you really need to say nums[0] = nums[0]; and nums[2] = nums[nums.length-1]; ?

    Those indexes are not going to change anyway, so removing those two lines of code wouldn't change the correctness of your solution.

    Stelios

    ReplyDelete
  3. CAN WE DO THIS

    public int[] maxEnd3(int[] nums) {
    if(nums[0]>nums[nums.length-1])
    return new int nums[]{nums[0] ,nums[0] ,nums[0]};
    else
    return new int nums[]{nums[nums.length-1] ,nums[nums.length-1] ,nums[nums.length-1]};
    }

    ReplyDelete
  4. public int[] maxEnd3(int[] nums) {
    int max=nums[0]>nums[nums.length-1]?nums[0]:nums[nums.length-1];
    return new int[]{max,max,max};
    }

    ReplyDelete
  5. int x = Math.max(nums[0], nums[2]);
    return new int[] {x,x,x};

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

    return new int[] { max, max, max };
    }

    ReplyDelete
  7. public int[] maxEnd3(int[] nums) {
    int[] myArr = new int[3];
    if(nums[0]>=nums[2]){
    myArr[0]=nums[0];
    myArr[1]=nums[0];
    myArr[2]=nums[0];
    }else if(nums[0]<nums[2]){
    myArr[0]=nums[2];
    myArr[1]=nums[2];
    myArr[2]=nums[2];
    }
    return myArr;
    }

    ReplyDelete
  8. public int[] maxEnd3(int[] nums) {
    if(nums[0]>nums[2])
    return new int [] {nums[0],nums[0],nums[0]};
    else if(nums[2]>nums[0])
    return new int [] {nums[2],nums[2],nums[2]};
    else if(nums[0]==nums[2])
    return new int [] {nums[2],nums[2],nums[2]};
    else return new int [] {nums[0],nums[1],nums[2]};
    }

    ReplyDelete
  9. public int[] maxEnd3(int[] nums) {

    int max = Math.max(nums[0], nums[nums.length-1]);

    for(int i = 0 ; i < nums.length ; i++){
    nums[i] = max;
    }

    return nums;
    }

    ReplyDelete
  10. int res[] = new int[nums.length];
    int max = 0;
    if(nums[0] > nums[nums.length-1] ){
    max = nums[0];
    }else{
    max = nums[nums.length-1];
    }
    for(i=0; i < res.length; i++){
    res[i]= max;
    }

    return res;

    ReplyDelete
  11. public int[] maxEnd3(int[] nums) {
    if (nums[0]>nums[2])
    return (new int [] {nums[0],nums[0],nums[0]});
    else
    return (new int [] {nums[2],nums[2],nums[2]});

    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact