Solved

bigDiff challenge

Posted on 2016-08-01
17
74 Views
Last Modified: 2016-08-02
Hi,

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);
}

Open in new window


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
0
Comment
Question by:gudii9
  • 6
  • 5
  • 4
  • +2
17 Comments
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
You have a copy-paste error in line 12.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
Fixing the copy paste error alone won't fix it.
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
I know that; I'm trying to get him closer to the solution, without giving it to him.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
let me check
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
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.
0
 
LVL 14

Expert Comment

by:CPColin
Comment Utility
If you want to answer this question in your way, go for it. I'm going to answer my way.
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
I did that already.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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);
}

Open in new window


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      
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 7

Author Comment

by:gudii9
Comment Utility
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);
}

Open in new window


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
0
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 250 total points
Comment Utility
Try printing the values of large and small at the end of each iteration of the loops and see how they change as your code processes one of the tests. You should be able to pick out what's going wrong pretty quickly.
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
i think it does compare last one which is wrong i guess instead it should keep value some where and compare each time to upate
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
small = large
0
 
LVL 27

Expert Comment

by:d-glitch
Comment Utility
You seem to have posted eight Challenge questions in 40 minutes.
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??
0
 
LVL 31

Accepted Solution

by:
awking00 earned 250 total points
Comment Utility
CodingBat doesn't always point you in the best direction. Rather than dealing with the min and max methods for class, you could just use the sort method for arrays.
int[] nums = {2,7,9,1};
Arrays.sort(nums);
return nums[nums.length - 1] - nums[0];
0
 
LVL 16

Expert Comment

by:krakatoa
Comment Utility
That's great . . . but when someone can't even handle the loops effectively yet, it's like missing out a whole chunk of basic CS jumping straight to these meta methods.
1
 
LVL 7

Author Comment

by:gudii9
Comment Utility
Arrays.sort(nums);
return nums[nums.length - 1] - nums[0];

Open in new window

this is awesome approach. just sort find min and max and find difference
0
 
LVL 7

Author Comment

by:gudii9
Comment Utility
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;
}

Open in new window


Above also all passing tests
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now