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) { int larger = Math.max(nums[0], nums[2]); nums[0] = larger; nums[1] = larger; nums[2] = larger; return nums; }
public int[] maxEnd3(int[]nums){
ReplyDeleteint z = Math.max(nums[0], nums[nums.length-1]);
for(int x = 0; x<nums.length; x++)
nums[x] = z;
return nums;
}
int max = values[0];
ReplyDeletefor (int index = 0 ; index < values.length ; index++)
{
max = Math.max(max, values[index]);
}
sorry its this one
ReplyDeleteint max = Math.max(nums[0], nums[2]);
int arr[] = {max, max, max};
return arr;
Sure, but it says to return the "changed" array. You modify it in-place.
Deletepublic int[] maxEnd3(int[] nums) {
ReplyDeletereturn new int[]{Math.max(nums[0], nums[2]), Math.max(nums[0], nums[2]), Math.max(nums[0], nums[2])};
}
public int[] maxEnd3(int[] nums) {
ReplyDeleteint max = Math.max(nums[0], nums[2]);
return new int[]{max, max, max};
}
int max = Math.max(nums[0], nums[2]);
ReplyDeletenums[0] = nums[1] = nums[2] = max;
return nums;
public int[] maxEnd3(int[] nums) {
ReplyDeleteint max =0;
if(nums[0]>nums[nums.length-1]){
max=nums[0];
}else{
max=nums[nums.length-1];
}
for(int i=0;i<nums.length;i++){
nums[i]=max;
}
return nums;
}
public int[] maxEnd3(int[] nums) {
ReplyDeleteif(nums[0]>nums[2])
{
int a[]={(nums[0]),(nums[0]),(nums[0])};
return a;
}
else
{
int b[]={(nums[2]),(nums[2]),(nums[2])};
return b;
}
}