Last active
July 26, 2018 17:19
-
-
Save Zyro9922/c93aedd83ea72d7c78028c4401ff2029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int centeredAverage(int[] nums) { | |
int max = nums[0]; | |
int min = nums[0]; | |
int countMax = 0, countMin = 0; | |
for (int i = 0; i < nums.length; i++) { | |
if (nums[i] >= max) { | |
max = nums[i]; | |
} | |
if (min >= nums[i]) { | |
min = nums[i]; | |
} | |
} | |
for (int i = 0; i < nums.length; i++) { | |
if (max == nums[i]) { | |
countMax++; | |
} | |
if (min == nums[i]) { | |
countMin++; | |
} | |
} | |
int sum = 0; | |
int length = 0; | |
for(int i = 0 ; i < nums.length ;i++) | |
{ | |
sum = sum + nums[i]; | |
} | |
int reqSum = 0; | |
if(max == min){ | |
reqSum = sum - min*(countMin-1); | |
length = nums.length-countMin+1; | |
} | |
else{ | |
if(countMin>1) | |
{ | |
countMin--; | |
} | |
if(countMax>1) | |
{ | |
countMax--; | |
} | |
reqSum = sum - min*countMin - max*countMax; | |
length = nums.length-countMin-countMax; | |
} | |
int average = reqSum/(length); | |
return average; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment