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
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
krakatoaCommented:
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

0
gurpsbassiCommented:
Try this:

public static int[] changeValuesToLargest(final int[] inputData) {

        final int[] newData = Arrays.copyOf(inputData, inputData.length);

        int currentLargest = 0;
        for (int i = 0; i < inputData.length; i++) {
            if (inputData[i] > currentLargest) {
                currentLargest = inputData[i];
            }
            newData[i] = currentLargest;
            Arrays.fill(newData, 0, i, currentLargest);
        }

        return newData;
    }

Open in new window


Should work for any length array. Not thoroughly tested!
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

krakatoaCommented:
Not thoroughly tested!

I think the point of these tests is that they pass on the codingbat site.
0
gurpsbassiCommented:
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

0
awking00Commented:
public int[] maxEnd3(int[] nums) {
   int max = Math.max(nums[0],nums[2]);
   return new int[]{max, max, max};
}
0
gudii9Author Commented:
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};
}
0
krakatoaCommented:
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.
0
gurpsbassiCommented:
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.
0
krakatoaCommented:
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.
0
gurpsbassiCommented:
I was referring to the line of code:
int max = Math.max(nums[0],nums[2]);

It assumes length of 3.
0
ozoCommented:
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.
0
krakatoaCommented:
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"
0
awking00Commented:
>>The challenge is, given an array of ints length 3, to find the max between the first and last (i.e. third)elements<<
0
krakatoaCommented:
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.
0
awking00Commented:
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.
0
krakatoaCommented:
That's correct. But loose-coupling / abstracting should always be implemented where possible as best practice.
0
gudii9Author Commented:
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
0
gurpsbassiCommented:
we are filling the array with the value of the substitute parameter.

Read docs.
0
gudii9Author Commented:
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?
0
gudii9Author Commented:
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
0
ozoCommented:
nums[0] > nums[nums.length - 1] ? nums[0] :nums[nums.length - 1];
is equivalent to
Math.max(nums[0],nums[nums.lenghth-1])
0
gurpsbassiCommented:
are we using above fill method from API?

Yes
0
gurpsbassiCommented:
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.
0
gudii9Author Commented:
when i highlight as attached then i can see the point
highlight.png
0
gudii9Author Commented:
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
0
gurpsbassiCommented:
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.
0
ozoCommented:
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
0
gurpsbassiCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.