Integer divisions of a number
Clash Royale CLAN TAG#URR8PPP
up vote
5
down vote
favorite
How do I split a number into equal parts or as close to equal as possible. See example below:
If I have a value of 61 that i want to distribute between two groups, it would be 30.5 and 30.5. Doubles (decimals) are no good, so in this regard the closest split is 30 and 31.
Similarly 42 / 5 = 8.4, however I need the system to return (8, 8, 8, 9, 9) which is the closest split with whole numbers.
Solved it guys:
if(sum % numberOfTeams != 0)
al.add(0, sNOT);
for(int i = 0; i < numberOfTeams - 1; i++)
int remover = sum - sNOT;
if(remover % (sNOT + 1) == 0)
al.add(i+1, sNOT + 1);
else
al.add(i + 1, sNOT);
}
java algorithm math numbers double
add a comment |Â
up vote
5
down vote
favorite
How do I split a number into equal parts or as close to equal as possible. See example below:
If I have a value of 61 that i want to distribute between two groups, it would be 30.5 and 30.5. Doubles (decimals) are no good, so in this regard the closest split is 30 and 31.
Similarly 42 / 5 = 8.4, however I need the system to return (8, 8, 8, 9, 9) which is the closest split with whole numbers.
Solved it guys:
if(sum % numberOfTeams != 0)
al.add(0, sNOT);
for(int i = 0; i < numberOfTeams - 1; i++)
int remover = sum - sNOT;
if(remover % (sNOT + 1) == 0)
al.add(i+1, sNOT + 1);
else
al.add(i + 1, sNOT);
}
java algorithm math numbers double
5
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
3
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
How do I split a number into equal parts or as close to equal as possible. See example below:
If I have a value of 61 that i want to distribute between two groups, it would be 30.5 and 30.5. Doubles (decimals) are no good, so in this regard the closest split is 30 and 31.
Similarly 42 / 5 = 8.4, however I need the system to return (8, 8, 8, 9, 9) which is the closest split with whole numbers.
Solved it guys:
if(sum % numberOfTeams != 0)
al.add(0, sNOT);
for(int i = 0; i < numberOfTeams - 1; i++)
int remover = sum - sNOT;
if(remover % (sNOT + 1) == 0)
al.add(i+1, sNOT + 1);
else
al.add(i + 1, sNOT);
}
java algorithm math numbers double
How do I split a number into equal parts or as close to equal as possible. See example below:
If I have a value of 61 that i want to distribute between two groups, it would be 30.5 and 30.5. Doubles (decimals) are no good, so in this regard the closest split is 30 and 31.
Similarly 42 / 5 = 8.4, however I need the system to return (8, 8, 8, 9, 9) which is the closest split with whole numbers.
Solved it guys:
if(sum % numberOfTeams != 0)
al.add(0, sNOT);
for(int i = 0; i < numberOfTeams - 1; i++)
int remover = sum - sNOT;
if(remover % (sNOT + 1) == 0)
al.add(i+1, sNOT + 1);
else
al.add(i + 1, sNOT);
}
java algorithm math numbers double
edited Aug 22 at 18:59
asked Aug 19 at 10:41
M.Shiz
224
224
5
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
3
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20
add a comment |Â
5
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
3
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20
5
5
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
3
3
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
3
down vote
Here is a solution which entirely uses Arrays
, a little shorter with no loops. Validity check should also be applied as suggested above
int value = 42;
int groups = 5;
int residue = value % groups;
int res = new int[groups];
int division = value / groups;
Arrays.fill(res, 0, residue, division +1);
Arrays.fill(res, residue, res.length, division);
System.out.println(Arrays.toString(res));
add a comment |Â
up vote
2
down vote
Here's one solution:
public static int closestSplit(int intToSplit, int noOfGroups)
int result = new int[noOfGroups];
Arrays.fill(result, intToSplit / noOfGroups);
for (int i = 0; i < intToSplit % noOfGroups; i++)
result[i]++;
return result;
// usage:
System.out.println(Arrays.toString(closestSplit(42, 5)));
Basically, it creates an array of length noOfGroups
first, then fills that with the integer division of intToSplit
and noOfGroups
. Next, it adds one to the first intToSplit mod noOfGroups
elements of the array.
If you require the result to be ordered in ascending order, you can loop from the end of the array or use Arrays.sort
.
add a comment |Â
up vote
1
down vote
The major thing you need to consider while solving this question is remainders.
From the example above, we intend to split 61 into 2, 61 / 2
gives a remainder of 1. Since we're splitting into two groups, only 1 out of these 2 groups have to be ((int) 61 / 2) + 1
. The remaining (one) group can be ((int) 61 / 2)
.
Also, consider splitting 42 into 5, 42 / 5
gives a remainder of 2. Since, we're splitting into 5 groups, only 2 out of these 5 groups have to be ((int) 42 / 5) + 1
. The remaining (three) groups can be ((int) 42 / 5)
.
Here's the code:
public int spitNumbers(int number, int groups)
int result = new int[groups]; //The array we'd return at the end
int shift = number % groups; //I used this to check the remainder.
for(int i = 0; i < result.length; i++)
/* The if-statement below checks for two things:
- if the remainder is zero
- if we've reached the shifting stage.
*/
if((shift == 0)
return result;
I hope this helps.. Merry coding!
add a comment |Â
up vote
0
down vote
Please have a look at this solution, I little tested, it works.
public static int number(int number, int divider)
p.s. little simplfied
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Here is a solution which entirely uses Arrays
, a little shorter with no loops. Validity check should also be applied as suggested above
int value = 42;
int groups = 5;
int residue = value % groups;
int res = new int[groups];
int division = value / groups;
Arrays.fill(res, 0, residue, division +1);
Arrays.fill(res, residue, res.length, division);
System.out.println(Arrays.toString(res));
add a comment |Â
up vote
3
down vote
Here is a solution which entirely uses Arrays
, a little shorter with no loops. Validity check should also be applied as suggested above
int value = 42;
int groups = 5;
int residue = value % groups;
int res = new int[groups];
int division = value / groups;
Arrays.fill(res, 0, residue, division +1);
Arrays.fill(res, residue, res.length, division);
System.out.println(Arrays.toString(res));
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Here is a solution which entirely uses Arrays
, a little shorter with no loops. Validity check should also be applied as suggested above
int value = 42;
int groups = 5;
int residue = value % groups;
int res = new int[groups];
int division = value / groups;
Arrays.fill(res, 0, residue, division +1);
Arrays.fill(res, residue, res.length, division);
System.out.println(Arrays.toString(res));
Here is a solution which entirely uses Arrays
, a little shorter with no loops. Validity check should also be applied as suggested above
int value = 42;
int groups = 5;
int residue = value % groups;
int res = new int[groups];
int division = value / groups;
Arrays.fill(res, 0, residue, division +1);
Arrays.fill(res, residue, res.length, division);
System.out.println(Arrays.toString(res));
answered Aug 19 at 13:05
Anton.P
1,47031123
1,47031123
add a comment |Â
add a comment |Â
up vote
2
down vote
Here's one solution:
public static int closestSplit(int intToSplit, int noOfGroups)
int result = new int[noOfGroups];
Arrays.fill(result, intToSplit / noOfGroups);
for (int i = 0; i < intToSplit % noOfGroups; i++)
result[i]++;
return result;
// usage:
System.out.println(Arrays.toString(closestSplit(42, 5)));
Basically, it creates an array of length noOfGroups
first, then fills that with the integer division of intToSplit
and noOfGroups
. Next, it adds one to the first intToSplit mod noOfGroups
elements of the array.
If you require the result to be ordered in ascending order, you can loop from the end of the array or use Arrays.sort
.
add a comment |Â
up vote
2
down vote
Here's one solution:
public static int closestSplit(int intToSplit, int noOfGroups)
int result = new int[noOfGroups];
Arrays.fill(result, intToSplit / noOfGroups);
for (int i = 0; i < intToSplit % noOfGroups; i++)
result[i]++;
return result;
// usage:
System.out.println(Arrays.toString(closestSplit(42, 5)));
Basically, it creates an array of length noOfGroups
first, then fills that with the integer division of intToSplit
and noOfGroups
. Next, it adds one to the first intToSplit mod noOfGroups
elements of the array.
If you require the result to be ordered in ascending order, you can loop from the end of the array or use Arrays.sort
.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Here's one solution:
public static int closestSplit(int intToSplit, int noOfGroups)
int result = new int[noOfGroups];
Arrays.fill(result, intToSplit / noOfGroups);
for (int i = 0; i < intToSplit % noOfGroups; i++)
result[i]++;
return result;
// usage:
System.out.println(Arrays.toString(closestSplit(42, 5)));
Basically, it creates an array of length noOfGroups
first, then fills that with the integer division of intToSplit
and noOfGroups
. Next, it adds one to the first intToSplit mod noOfGroups
elements of the array.
If you require the result to be ordered in ascending order, you can loop from the end of the array or use Arrays.sort
.
Here's one solution:
public static int closestSplit(int intToSplit, int noOfGroups)
int result = new int[noOfGroups];
Arrays.fill(result, intToSplit / noOfGroups);
for (int i = 0; i < intToSplit % noOfGroups; i++)
result[i]++;
return result;
// usage:
System.out.println(Arrays.toString(closestSplit(42, 5)));
Basically, it creates an array of length noOfGroups
first, then fills that with the integer division of intToSplit
and noOfGroups
. Next, it adds one to the first intToSplit mod noOfGroups
elements of the array.
If you require the result to be ordered in ascending order, you can loop from the end of the array or use Arrays.sort
.
edited Aug 19 at 11:18
zlakad
9171614
9171614
answered Aug 19 at 10:50
Sweeper
54.6k960117
54.6k960117
add a comment |Â
add a comment |Â
up vote
1
down vote
The major thing you need to consider while solving this question is remainders.
From the example above, we intend to split 61 into 2, 61 / 2
gives a remainder of 1. Since we're splitting into two groups, only 1 out of these 2 groups have to be ((int) 61 / 2) + 1
. The remaining (one) group can be ((int) 61 / 2)
.
Also, consider splitting 42 into 5, 42 / 5
gives a remainder of 2. Since, we're splitting into 5 groups, only 2 out of these 5 groups have to be ((int) 42 / 5) + 1
. The remaining (three) groups can be ((int) 42 / 5)
.
Here's the code:
public int spitNumbers(int number, int groups)
int result = new int[groups]; //The array we'd return at the end
int shift = number % groups; //I used this to check the remainder.
for(int i = 0; i < result.length; i++)
/* The if-statement below checks for two things:
- if the remainder is zero
- if we've reached the shifting stage.
*/
if((shift == 0)
return result;
I hope this helps.. Merry coding!
add a comment |Â
up vote
1
down vote
The major thing you need to consider while solving this question is remainders.
From the example above, we intend to split 61 into 2, 61 / 2
gives a remainder of 1. Since we're splitting into two groups, only 1 out of these 2 groups have to be ((int) 61 / 2) + 1
. The remaining (one) group can be ((int) 61 / 2)
.
Also, consider splitting 42 into 5, 42 / 5
gives a remainder of 2. Since, we're splitting into 5 groups, only 2 out of these 5 groups have to be ((int) 42 / 5) + 1
. The remaining (three) groups can be ((int) 42 / 5)
.
Here's the code:
public int spitNumbers(int number, int groups)
int result = new int[groups]; //The array we'd return at the end
int shift = number % groups; //I used this to check the remainder.
for(int i = 0; i < result.length; i++)
/* The if-statement below checks for two things:
- if the remainder is zero
- if we've reached the shifting stage.
*/
if((shift == 0)
return result;
I hope this helps.. Merry coding!
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The major thing you need to consider while solving this question is remainders.
From the example above, we intend to split 61 into 2, 61 / 2
gives a remainder of 1. Since we're splitting into two groups, only 1 out of these 2 groups have to be ((int) 61 / 2) + 1
. The remaining (one) group can be ((int) 61 / 2)
.
Also, consider splitting 42 into 5, 42 / 5
gives a remainder of 2. Since, we're splitting into 5 groups, only 2 out of these 5 groups have to be ((int) 42 / 5) + 1
. The remaining (three) groups can be ((int) 42 / 5)
.
Here's the code:
public int spitNumbers(int number, int groups)
int result = new int[groups]; //The array we'd return at the end
int shift = number % groups; //I used this to check the remainder.
for(int i = 0; i < result.length; i++)
/* The if-statement below checks for two things:
- if the remainder is zero
- if we've reached the shifting stage.
*/
if((shift == 0)
return result;
I hope this helps.. Merry coding!
The major thing you need to consider while solving this question is remainders.
From the example above, we intend to split 61 into 2, 61 / 2
gives a remainder of 1. Since we're splitting into two groups, only 1 out of these 2 groups have to be ((int) 61 / 2) + 1
. The remaining (one) group can be ((int) 61 / 2)
.
Also, consider splitting 42 into 5, 42 / 5
gives a remainder of 2. Since, we're splitting into 5 groups, only 2 out of these 5 groups have to be ((int) 42 / 5) + 1
. The remaining (three) groups can be ((int) 42 / 5)
.
Here's the code:
public int spitNumbers(int number, int groups)
int result = new int[groups]; //The array we'd return at the end
int shift = number % groups; //I used this to check the remainder.
for(int i = 0; i < result.length; i++)
/* The if-statement below checks for two things:
- if the remainder is zero
- if we've reached the shifting stage.
*/
if((shift == 0)
return result;
I hope this helps.. Merry coding!
edited Aug 19 at 11:52
answered Aug 19 at 11:33
Taslim
1,02921024
1,02921024
add a comment |Â
add a comment |Â
up vote
0
down vote
Please have a look at this solution, I little tested, it works.
public static int number(int number, int divider)
p.s. little simplfied
add a comment |Â
up vote
0
down vote
Please have a look at this solution, I little tested, it works.
public static int number(int number, int divider)
p.s. little simplfied
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Please have a look at this solution, I little tested, it works.
public static int number(int number, int divider)
p.s. little simplfied
Please have a look at this solution, I little tested, it works.
public static int number(int number, int divider)
p.s. little simplfied
edited Aug 19 at 11:42
answered Aug 19 at 11:34
ImZ
266
266
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%2fstackoverflow.com%2fquestions%2f51916824%2finteger-divisions-of-a-number%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
5
Not taking away from an interesting problem, but curious as to why a question with no effort gets several upvotes?
â achAmháin
Aug 19 at 11:13
3
I agree entirely with @Dukeling.. I had a little squabble about this on the meta site yesterday. Interesting questions are more likely to receive upvotes (regardless of shown effort) than uninteresting ones.
â Taslim
Aug 19 at 11:58
@achAmháin That is typical of the algorithm tag.
â m69
Aug 19 at 13:20