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);
}
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
I know that; I'm trying to get him closer to the solution, without giving it to him.
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);
}
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);
}
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
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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
python question | 5 | 57 | |
Java Filesystem manipulation of Word Docx file (find replace text) | 3 | 38 | |
looking for unix program to search some keywords in side EAR WAR and JAR files | 10 | 49 | |
Not needed | 13 | 53 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
16 Experts available now in Live!