The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Hi,

I am working on below challenge

http://codingbat.com/prob/p196640

I wrote my code as below

I am failing few tests

How to improve my design, approach, code? please advise

I am working on below challenge

http://codingbat.com/prob/p196640

I wrote my code as below

```
public int bigDiff(int[] nums) {
int large=0;
int small=0;
for(int n:nums){
large= Math.max(large, n);
//large=n;
}
for(int n:nums){
small= Math.min(large, n);
// small=n;
}
return (large-small);
}
```

I am failing few tests

Expected Run

bigDiff([10, 3, 5, 6]) → 7 4 X

bigDiff([7, 2, 10, 9]) → 8 1 X

bigDiff([2, 10, 7, 2]) → 8 8 OK

bigDiff([2, 10]) → 8 0 X

bigDiff([10, 2]) → 8 8 OK

bigDiff([10, 0]) → 10 10 OK

bigDiff([2, 3]) → 1 0 X

bigDiff([2, 2]) → 0 0 OK

bigDiff([2]) → 0 0 OK

bigDiff([5, 1, 6, 1, 9, 9]) → 8 0 X

bigDiff([7, 6, 8, 5]) → 3 3 OK

bigDiff([7, 7, 6, 8, 5, 5, 6]) → 3 2 X

other tests

X

How to improve my design, approach, code? please advise

I know that; I'm trying to get him closer to the solution, without giving it to him.

then you should have said that there are several areas that need attention. Doing it without that hint is not the right way.

```
public int bigDiff(int[] nums) {
int large=0;
int small=0;
for(int n:nums){
large= Math.max(large, n);
//large=n;
}
for(int n:nums){
small= Math.min(small, n);
// small=n;
}
return (large-small);
}
```

i see that issue and fixed it.

Failing below tests

Expected Run

bigDiff([10, 3, 5, 6]) → 7 10 X

bigDiff([7, 2, 10, 9]) → 8 10 X

bigDiff([2, 10, 7, 2]) → 8 10 X

bigDiff([2, 10]) → 8 10 X

bigDiff([10, 2]) → 8 10 X

bigDiff([10, 0]) → 10 10 OK

bigDiff([2, 3]) → 1 3 X

bigDiff([2, 2]) → 0 2 X

bigDiff([2]) → 0 2 X

bigDiff([5, 1, 6, 1, 9, 9]) → 8 9 X

bigDiff([7, 6, 8, 5]) → 3 8 X

bigDiff([7, 7, 6, 8, 5, 5, 6]) → 3 8 X

other tests

X

```
public int bigDiff(int[] nums) {
int large=0;
int small=0;
int largest=0;
int smallest=0;
int result=0;
int len=nums.length;
for(int i=0;i<len-1;i++){
large= Math.max(nums[i], nums[i+1]);
//large=n;
// if(large>largest){
//result=l
}
for(int i=0;i<len-1;i++){
small= Math.min(nums[i], nums[i+1]);
// small=n;
}
return (large-small);
}
```

with old for loop i passed few more tests but still failing some

Expected Run

bigDiff([10, 3, 5, 6]) → 7 1 X

bigDiff([7, 2, 10, 9]) → 8 1 X

bigDiff([2, 10, 7, 2]) → 8 5 X

bigDiff([2, 10]) → 8 8 OK

bigDiff([10, 2]) → 8 8 OK

bigDiff([10, 0]) → 10 10 OK

bigDiff([2, 3]) → 1 1 OK

bigDiff([2, 2]) → 0 0 OK

bigDiff([2]) → 0 0 OK

bigDiff([5, 1, 6, 1, 9, 9]) → 8 0 X

bigDiff([7, 6, 8, 5]) → 3 3 OK

bigDiff([7, 7, 6, 8, 5, 5, 6]) → 3 1 X

other tests

X

please advise

It doesn't seem like you are giving these questions any thought at all.

Why don't you try one at a time and see if you can actually learn something??

```
Arrays.sort(nums);
return nums[nums.length - 1] - nums[0];
```

this is awesome approach. just sort find min and max and find difference
```
public int bigDiff(int[] nums) {
int max=nums[0];
int min=nums[0];
for(int i=0;i<nums.length;i++){
if(max<=nums[i]){
max=nums[i];
}
if(min>=nums[i]){
min=nums[i];
}
}
return max-min;
}
```

Above also all passing tests

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.

All Courses

From novice to tech pro — start learning today.

int[] nums = {2,7,9,1};

Arrays.sort(nums);

return nums[nums.length - 1] - nums[0];