Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

maxEnd3 challenge

Hi,

I am working on below challenge

http://codingbat.com/prob/p146256

public int[] maxEnd3(int[] nums) {

int numsNew[]=new int[3];

if(nums[2]>nums[0]){

numsNew[0]=nums[2];
numsNew[1]=nums[2];
numsNew[2]=nums[2];
return numsNew;
}
else 
numsNew[0]=nums[0];
numsNew[1]=nums[0];
numsNew[2]=nums[0];
return numsNew;
  
}

Open in new window


i wrote as above and passed all tests. I wonder how i can improve above code or any alternate ways to do it. please advise
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Not thoroughly tested!

I think the point of these tests is that they pass on the codingbat site.
Thanks for the pointer.

Try this version. Seems to pass on the codingbat site:

public int[] maxEnd3(int[] nums) {
                 final int[] newData = Arrays.copyOf(nums, nums.length);

                int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];
                Arrays.fill(newData, 0, nums.length, substitute);
                 return newData;
}

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gudii9

ASKER

public int[] maxEnd3(int[] nums) {
                 final int[] newData = Arrays.copyOf(nums, nums.length);

                int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];
                Arrays.fill(newData, 0, nums.length, substitute);
                 return newData;
}









nums[0]=nums[1]=nums[2]=nums[nums[0]<nums[2]?2:0];];
  return nums;



public int[] maxEnd3(int[] nums) {

if(nums[0]>nums[nums.length-1]){for(int r=0;r<nums.length;r++){nums[r]=nums[0];}}

else{
for(int r=0;r<nums.length;r++){nums[r]=nums[nums.length-1];}

}
return nums;  
}

Open in new window


can you please calrify on above 3 solutions. i was not clear what is going in the code


Below is simple and straight forward to me

public int[] maxEnd3(int[] nums) {
   int max = Math.max(nums[0],nums[2]);
   return new int[]{max, max, max};
}
Your max approach is a good approach. "Keep things as simple as possible" is the best way - they are usually then faster and easier to remember and for others to understand.
Ok I will try and explain:

Step 1: I create a copy of the original array.
                final int[] newData = Arrays.copyOf(nums, nums.length);

Step 2: Work out which value is larger out of the first and last element and store that value in a local variable called substitute.
             int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];

Step 3: Use the java.util.Arrays class to assign the value of the substitute variable to each element of the newData array in the specified range from 0 up to the length of the array.
            Arrays.fill(newData, 0, nums.length, substitute);
                 
You max approach will only work for arrays of size 3, but I guess that's the requirements from CodingBat.
You max approach will only work for arrays of size 3

Err, Math.max(int, int) will only compare two ints at once, so it has nothing to do with the size of the array being processed.

And Math.max can be nested, so that you can take the max of any number of ints if you want to go that route.
I was referring to the line of code:
int max = Math.max(nums[0],nums[2]);

It assumes length of 3.
Math.max(nums[0],nums[2]) will only work for arrays of size 3,
But any array of non-zero size will have one first element and one last element, so Math.max(nums[0],nums[nums.lenghth-1]) will work for arrays of any non-zero size.
new int[]{max, max, max}; will only work for arrays of size 3, and I wouldn't want to use that approach for arrays of size much larger than 3,
But the requirements specification says "Given an array of ints length 3" so the fact that it will only work for arrays of size 3 may be no more of an issue than the fact that it will only work for arrays of ints
An often nice property of code could be that it can be easily modified to fit modifications of the requirements specification,
but since there are many different ways a requirements specification might be modified, in absence of an explicit set of alternate specifications, which one you choose to be nice with can be a matter of personal taste.
The challenge is to find the max between the first and last elements - it has nothing to do with whether there are 3 of them or 10,003.

"figure out which is larger between the first and last elements in the array"
>>The challenge is, given an array of ints length 3, to find the max between the first and last (i.e. third)elements<<
I don't know if your point was directed at me, but the length of the array has nothing to do with the length of 3. If you query nums[0] and nums[nums.length-1], then whether the passed-in array is 3 or 33 length, you will get the correct answer, and it won't be hard-wired like : nums[0] and nums[2] is.
I agree that using nums[0] and nums[nums.length - 1] would get the first and last elements of an array and make it adaptable to int arrays of any length. It's just that the challenge specifically stated int arrays of length 3, so the indexes will always be 0 and 2.
That's correct. But loose-coupling / abstracting should always be implemented where possible as best practice.
Avatar of gudii9

ASKER

Step 3: Use the java.util.Arrays class to assign the value of the substitute variable to each element of the newData array in the specified range from 0 up to the length of the array.
            Arrays.fill(newData, 0, nums.length, substitute);
                 

i got step1 and step2.

i have not understood step3 above. what are we filling by passing newData array which is copied one, then 0 and length and substitute?
 please advise
we are filling the array with the value of the substitute parameter.

Read docs.
Avatar of gudii9

ASKER

static void	fill(int[] a, int fromIndex, int toIndex, int val)
Assigns the specified int value to each element of the specified range of the specified array of ints.

Open in new window


are we using above fill method from API?
Avatar of gudii9

ASKER

Step 2: Work out which value is larger out of the first and last element and store that value in a local variable called substitute.
             int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];

how did we work out above large value logic?

please advise
nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];
is equivalent to
Math.max(nums[0],nums[nums.lenghth-1])
are we using above fill method from API?

Yes
how did we work out above large value logic?

int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];

Open in new window


Is the same as saying:

int substitute;
if(nums[0] > nums[nums.length -1]){
               substitute = nums[0];
           }else{
                  substitute = nums[nums.length - 1];
}

Open in new window


or in pseudo code:
if first element is greater than last element then store the value of the first element in the substitute parameter
otherwise store the value of the last element in the substitute parameter.
Avatar of gudii9

ASKER

when i highlight as attached then i can see the point
highlight.png
Avatar of gudii9

ASKER

so we are overriding and filling each and every value of newData with substitute?

Step 3: Use the java.util.Arrays class to assign the value of the substitute variable to each element of the newData array in the specified range from 0 up to the length of the array.

why we need newData here?

public int[] maxEnd3(int[] nums) {
                 final int[] newData = Arrays.copyOf(nums, nums.length);

                int substitute = nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];
                Arrays.fill(newData, 0, nums.length, substitute);
                 return newData;
}









nums[0]=nums[1]=nums[2]=nums[nums[0]<nums[2]?2:0];];
  return nums;



public int[] maxEnd3(int[] nums) {

if(nums[0]>nums[nums.length-1]){for(int r=0;r<nums.length;r++){nums[r]=nums[0];}}

else{
for(int r=0;r<nums.length;r++){nums[r]=nums[nums.length-1];}

}
return nums;  
}

Open in new window


please advise
why we need newData here?

Do you want the method to return the same array with modified content or a totally different array with the new data in it?
I prefer to return a seperate array.
I prefer to return a seperate array.
While this is a reasonable preference, the wording of http://codingbat.com/prob/p146256 could suggest wanting to return the same array with modified content
While this is a reasonable preference, the wording of http://codingbat.com/prob/p146256 could suggest wanting to return the same array with modified content

true but  the method ought to return void in that case.