Award Budget Cuts implementation in Java
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
Award Budget Cuts
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem theyâÂÂre facing. Originally, the committee planned to give N research grants this year. However, due to spending cutbacks, the budget was reduced to newBudget dollars and now they need to reallocate the grants. The committee made a decision that theyâÂÂd like to impact as few grant recipients as possible by applying a maximum cap on all grants. Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, wonâÂÂt be impacted.
Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).
Example:
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
output: 47 # and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
My approach:
import java.util.Arrays;
class Solution
static double findGrantsCap(double grantsArray, double newBudget)
// your code goes here
int len = grantsArray.length;
Arrays.sort(grantsArray);
//Calculate the newBudget
double modBudget = newBudget;
double cap;
double sum = 0;
double avg;
for (double elem: grantsArray)
sum += elem;
avg = sum/len;
if( avg == (int)newBudget)
return (newBudget)/len;
else
Arrays.sort(grantsArray);
modBudget -= grantsArray[0];
cap = modBudget/(len - 1);
for( int i = 1; i < len-1; i++ )
System.out.println(cap);
if( (int)cap > grantsArray[i] )
modBudget -= grantsArray[i];
cap = modBudget/(len - i - 1);
return cap;
public static void main(String args)
double grantsArray = 2,100,50,120,167;
double newBudget = 400;
System.out.println(findGrantsCap(grantsArray,newBudget));
I have the following questions with regards to the above code:
1) Is there a better way to attempt this question?
2) Is my solution satisfying all the test cases and why is it failing if any?
3) How can i further improve the algorithm, time or space wise?
4) Have I made any gross violations to the Java Coding conventions?
Reference
java beginner interview-questions complexity
add a comment |Â
up vote
0
down vote
favorite
Award Budget Cuts
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem theyâÂÂre facing. Originally, the committee planned to give N research grants this year. However, due to spending cutbacks, the budget was reduced to newBudget dollars and now they need to reallocate the grants. The committee made a decision that theyâÂÂd like to impact as few grant recipients as possible by applying a maximum cap on all grants. Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, wonâÂÂt be impacted.
Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).
Example:
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
output: 47 # and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
My approach:
import java.util.Arrays;
class Solution
static double findGrantsCap(double grantsArray, double newBudget)
// your code goes here
int len = grantsArray.length;
Arrays.sort(grantsArray);
//Calculate the newBudget
double modBudget = newBudget;
double cap;
double sum = 0;
double avg;
for (double elem: grantsArray)
sum += elem;
avg = sum/len;
if( avg == (int)newBudget)
return (newBudget)/len;
else
Arrays.sort(grantsArray);
modBudget -= grantsArray[0];
cap = modBudget/(len - 1);
for( int i = 1; i < len-1; i++ )
System.out.println(cap);
if( (int)cap > grantsArray[i] )
modBudget -= grantsArray[i];
cap = modBudget/(len - i - 1);
return cap;
public static void main(String args)
double grantsArray = 2,100,50,120,167;
double newBudget = 400;
System.out.println(findGrantsCap(grantsArray,newBudget));
I have the following questions with regards to the above code:
1) Is there a better way to attempt this question?
2) Is my solution satisfying all the test cases and why is it failing if any?
3) How can i further improve the algorithm, time or space wise?
4) Have I made any gross violations to the Java Coding conventions?
Reference
java beginner interview-questions complexity
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Award Budget Cuts
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem theyâÂÂre facing. Originally, the committee planned to give N research grants this year. However, due to spending cutbacks, the budget was reduced to newBudget dollars and now they need to reallocate the grants. The committee made a decision that theyâÂÂd like to impact as few grant recipients as possible by applying a maximum cap on all grants. Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, wonâÂÂt be impacted.
Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).
Example:
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
output: 47 # and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
My approach:
import java.util.Arrays;
class Solution
static double findGrantsCap(double grantsArray, double newBudget)
// your code goes here
int len = grantsArray.length;
Arrays.sort(grantsArray);
//Calculate the newBudget
double modBudget = newBudget;
double cap;
double sum = 0;
double avg;
for (double elem: grantsArray)
sum += elem;
avg = sum/len;
if( avg == (int)newBudget)
return (newBudget)/len;
else
Arrays.sort(grantsArray);
modBudget -= grantsArray[0];
cap = modBudget/(len - 1);
for( int i = 1; i < len-1; i++ )
System.out.println(cap);
if( (int)cap > grantsArray[i] )
modBudget -= grantsArray[i];
cap = modBudget/(len - i - 1);
return cap;
public static void main(String args)
double grantsArray = 2,100,50,120,167;
double newBudget = 400;
System.out.println(findGrantsCap(grantsArray,newBudget));
I have the following questions with regards to the above code:
1) Is there a better way to attempt this question?
2) Is my solution satisfying all the test cases and why is it failing if any?
3) How can i further improve the algorithm, time or space wise?
4) Have I made any gross violations to the Java Coding conventions?
Reference
java beginner interview-questions complexity
Award Budget Cuts
The awards committee of your alma mater (i.e. your college/university) asked for your assistance with a budget allocation problem theyâÂÂre facing. Originally, the committee planned to give N research grants this year. However, due to spending cutbacks, the budget was reduced to newBudget dollars and now they need to reallocate the grants. The committee made a decision that theyâÂÂd like to impact as few grant recipients as possible by applying a maximum cap on all grants. Every grant initially planned to be higher than cap will now be exactly cap dollars. Grants less or equal to cap, obviously, wonâÂÂt be impacted.
Given an array grantsArray of the original grants and the reduced budget newBudget, write a function findGrantsCap that finds in the most efficient manner a cap such that the least number of recipients is impacted and that the new budget constraint is met (i.e. sum of the N reallocated grants equals to newBudget).
Example:
input: grantsArray = [2, 100, 50, 120, 1000], newBudget = 190
output: 47 # and given this cap the new grants array would be
# [2, 47, 47, 47, 47]. Notice that the sum of the
# new grants is indeed 190
My approach:
import java.util.Arrays;
class Solution
static double findGrantsCap(double grantsArray, double newBudget)
// your code goes here
int len = grantsArray.length;
Arrays.sort(grantsArray);
//Calculate the newBudget
double modBudget = newBudget;
double cap;
double sum = 0;
double avg;
for (double elem: grantsArray)
sum += elem;
avg = sum/len;
if( avg == (int)newBudget)
return (newBudget)/len;
else
Arrays.sort(grantsArray);
modBudget -= grantsArray[0];
cap = modBudget/(len - 1);
for( int i = 1; i < len-1; i++ )
System.out.println(cap);
if( (int)cap > grantsArray[i] )
modBudget -= grantsArray[i];
cap = modBudget/(len - i - 1);
return cap;
public static void main(String args)
double grantsArray = 2,100,50,120,167;
double newBudget = 400;
System.out.println(findGrantsCap(grantsArray,newBudget));
I have the following questions with regards to the above code:
1) Is there a better way to attempt this question?
2) Is my solution satisfying all the test cases and why is it failing if any?
3) How can i further improve the algorithm, time or space wise?
4) Have I made any gross violations to the Java Coding conventions?
Reference
java beginner interview-questions complexity
asked May 12 at 19:04
Anirudh Thatipelli
227211
227211
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41
add a comment |Â
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
1) Code from https://repl.it/@sixxta/Award-Budget-Cuts suggests you could sort array in descending order and keep decreasing maximum single grant until there is no overspending. I don't know if you version, starting with smallest value, works correctly.
2) for cases:
14,15,16,17,18,19, 97
14,15,16,17,18,19, 98
14,15,16,17,18,19, 99
your code returns:
17.33
17.66
18.5
While the proper answer is:
17
18
19
3) the algorithm is optimal in terms of time and space, unless the is some way to remove sorting that I am not aware of.
4) you are looking for exact solution, using floating point variables isn't a good idea
if( avg == (int)newBudget)
return (newBudget)/len;
This check doesn't make any sense. You probably wanted to compare with newBudget/len
but even then you would return 2
for 1,2,3,6
when right answer is 3
.
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
add a comment |Â
up vote
1
down vote
There is a better approach for that problem.
First start by sorting the array, next traverse the array, for every element that is lower than the cap
(your cap
starts as the avg
of budget/array.length
) reduce it from your current funds and recalculate the cap (i.e. now the cap
equals to budget/(array.length - (i+1))
).
Once you got to the end of the array or to the first element that is higher than your current cap
you just return the last cap you have in hand.
So all in all this is your solution:
static double findGrantsCap(double arr, double newBudget)
// your code goes here
Arrays.sort(arr); // O(N*LogN)
int n = arr.length;
double cap = newBudget/n;
for(int i = 0; i < n; i++) // O(N)
if(arr[i] < cap)
newBudget -= arr[i];
cap = (newBudget/(n-(1+i)));
else
arr[i] = cap;
return cap;
Adding all the numbers on the array will be the newBudget since for every lower number we subtract it from the total and everything above just gets capped (essentially gets to be the average from what we got left with divided with the total elements we got left with - adding those will be the remains of the budget we got up to them).
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
1) Code from https://repl.it/@sixxta/Award-Budget-Cuts suggests you could sort array in descending order and keep decreasing maximum single grant until there is no overspending. I don't know if you version, starting with smallest value, works correctly.
2) for cases:
14,15,16,17,18,19, 97
14,15,16,17,18,19, 98
14,15,16,17,18,19, 99
your code returns:
17.33
17.66
18.5
While the proper answer is:
17
18
19
3) the algorithm is optimal in terms of time and space, unless the is some way to remove sorting that I am not aware of.
4) you are looking for exact solution, using floating point variables isn't a good idea
if( avg == (int)newBudget)
return (newBudget)/len;
This check doesn't make any sense. You probably wanted to compare with newBudget/len
but even then you would return 2
for 1,2,3,6
when right answer is 3
.
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
add a comment |Â
up vote
1
down vote
1) Code from https://repl.it/@sixxta/Award-Budget-Cuts suggests you could sort array in descending order and keep decreasing maximum single grant until there is no overspending. I don't know if you version, starting with smallest value, works correctly.
2) for cases:
14,15,16,17,18,19, 97
14,15,16,17,18,19, 98
14,15,16,17,18,19, 99
your code returns:
17.33
17.66
18.5
While the proper answer is:
17
18
19
3) the algorithm is optimal in terms of time and space, unless the is some way to remove sorting that I am not aware of.
4) you are looking for exact solution, using floating point variables isn't a good idea
if( avg == (int)newBudget)
return (newBudget)/len;
This check doesn't make any sense. You probably wanted to compare with newBudget/len
but even then you would return 2
for 1,2,3,6
when right answer is 3
.
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
add a comment |Â
up vote
1
down vote
up vote
1
down vote
1) Code from https://repl.it/@sixxta/Award-Budget-Cuts suggests you could sort array in descending order and keep decreasing maximum single grant until there is no overspending. I don't know if you version, starting with smallest value, works correctly.
2) for cases:
14,15,16,17,18,19, 97
14,15,16,17,18,19, 98
14,15,16,17,18,19, 99
your code returns:
17.33
17.66
18.5
While the proper answer is:
17
18
19
3) the algorithm is optimal in terms of time and space, unless the is some way to remove sorting that I am not aware of.
4) you are looking for exact solution, using floating point variables isn't a good idea
if( avg == (int)newBudget)
return (newBudget)/len;
This check doesn't make any sense. You probably wanted to compare with newBudget/len
but even then you would return 2
for 1,2,3,6
when right answer is 3
.
1) Code from https://repl.it/@sixxta/Award-Budget-Cuts suggests you could sort array in descending order and keep decreasing maximum single grant until there is no overspending. I don't know if you version, starting with smallest value, works correctly.
2) for cases:
14,15,16,17,18,19, 97
14,15,16,17,18,19, 98
14,15,16,17,18,19, 99
your code returns:
17.33
17.66
18.5
While the proper answer is:
17
18
19
3) the algorithm is optimal in terms of time and space, unless the is some way to remove sorting that I am not aware of.
4) you are looking for exact solution, using floating point variables isn't a good idea
if( avg == (int)newBudget)
return (newBudget)/len;
This check doesn't make any sense. You probably wanted to compare with newBudget/len
but even then you would return 2
for 1,2,3,6
when right answer is 3
.
answered May 16 at 10:41
user158037
41136
41136
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
add a comment |Â
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
Thanks for the advice. @user158037. I will keep this important floating point issue in mind.
â Anirudh Thatipelli
May 19 at 16:06
add a comment |Â
up vote
1
down vote
There is a better approach for that problem.
First start by sorting the array, next traverse the array, for every element that is lower than the cap
(your cap
starts as the avg
of budget/array.length
) reduce it from your current funds and recalculate the cap (i.e. now the cap
equals to budget/(array.length - (i+1))
).
Once you got to the end of the array or to the first element that is higher than your current cap
you just return the last cap you have in hand.
So all in all this is your solution:
static double findGrantsCap(double arr, double newBudget)
// your code goes here
Arrays.sort(arr); // O(N*LogN)
int n = arr.length;
double cap = newBudget/n;
for(int i = 0; i < n; i++) // O(N)
if(arr[i] < cap)
newBudget -= arr[i];
cap = (newBudget/(n-(1+i)));
else
arr[i] = cap;
return cap;
Adding all the numbers on the array will be the newBudget since for every lower number we subtract it from the total and everything above just gets capped (essentially gets to be the average from what we got left with divided with the total elements we got left with - adding those will be the remains of the budget we got up to them).
add a comment |Â
up vote
1
down vote
There is a better approach for that problem.
First start by sorting the array, next traverse the array, for every element that is lower than the cap
(your cap
starts as the avg
of budget/array.length
) reduce it from your current funds and recalculate the cap (i.e. now the cap
equals to budget/(array.length - (i+1))
).
Once you got to the end of the array or to the first element that is higher than your current cap
you just return the last cap you have in hand.
So all in all this is your solution:
static double findGrantsCap(double arr, double newBudget)
// your code goes here
Arrays.sort(arr); // O(N*LogN)
int n = arr.length;
double cap = newBudget/n;
for(int i = 0; i < n; i++) // O(N)
if(arr[i] < cap)
newBudget -= arr[i];
cap = (newBudget/(n-(1+i)));
else
arr[i] = cap;
return cap;
Adding all the numbers on the array will be the newBudget since for every lower number we subtract it from the total and everything above just gets capped (essentially gets to be the average from what we got left with divided with the total elements we got left with - adding those will be the remains of the budget we got up to them).
add a comment |Â
up vote
1
down vote
up vote
1
down vote
There is a better approach for that problem.
First start by sorting the array, next traverse the array, for every element that is lower than the cap
(your cap
starts as the avg
of budget/array.length
) reduce it from your current funds and recalculate the cap (i.e. now the cap
equals to budget/(array.length - (i+1))
).
Once you got to the end of the array or to the first element that is higher than your current cap
you just return the last cap you have in hand.
So all in all this is your solution:
static double findGrantsCap(double arr, double newBudget)
// your code goes here
Arrays.sort(arr); // O(N*LogN)
int n = arr.length;
double cap = newBudget/n;
for(int i = 0; i < n; i++) // O(N)
if(arr[i] < cap)
newBudget -= arr[i];
cap = (newBudget/(n-(1+i)));
else
arr[i] = cap;
return cap;
Adding all the numbers on the array will be the newBudget since for every lower number we subtract it from the total and everything above just gets capped (essentially gets to be the average from what we got left with divided with the total elements we got left with - adding those will be the remains of the budget we got up to them).
There is a better approach for that problem.
First start by sorting the array, next traverse the array, for every element that is lower than the cap
(your cap
starts as the avg
of budget/array.length
) reduce it from your current funds and recalculate the cap (i.e. now the cap
equals to budget/(array.length - (i+1))
).
Once you got to the end of the array or to the first element that is higher than your current cap
you just return the last cap you have in hand.
So all in all this is your solution:
static double findGrantsCap(double arr, double newBudget)
// your code goes here
Arrays.sort(arr); // O(N*LogN)
int n = arr.length;
double cap = newBudget/n;
for(int i = 0; i < n; i++) // O(N)
if(arr[i] < cap)
newBudget -= arr[i];
cap = (newBudget/(n-(1+i)));
else
arr[i] = cap;
return cap;
Adding all the numbers on the array will be the newBudget since for every lower number we subtract it from the total and everything above just gets capped (essentially gets to be the average from what we got left with divided with the total elements we got left with - adding those will be the remains of the budget we got up to them).
edited Jul 23 at 17:25
t3chb0t
31.9k54195
31.9k54195
answered Jul 23 at 17:16
crazyPixel
1286
1286
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194272%2faward-budget-cuts-implementation-in-java%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
"Is my solution satisfying all the test cases and why is it failing if any": have you ran tests yourself?
â Koekje
May 12 at 20:52
@Koekje, Yes, I have run some tests given on the website and my algorithm passes most of them. I am unsure as in how I can solve for the remaining cases. My sincere apologies if this is defying the rules of this web forum.
â Anirudh Thatipelli
May 13 at 4:55
It is written that you should have code which works, to the best of your knowledge. Which you know it does not. So it would seem it does not completely conform. I have no idea how good/bad it is in this case, or what to do :D
â Koekje
May 13 at 23:41