Integer divisions of a number

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
5
down vote

favorite
1












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







}







share|improve this question


















  • 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














up vote
5
down vote

favorite
1












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







}







share|improve this question


















  • 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












up vote
5
down vote

favorite
1









up vote
5
down vote

favorite
1






1





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







}







share|improve this question














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







}









share|improve this question













share|improve this question




share|improve this question








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












  • 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












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





share|improve this answer



























    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.






    share|improve this answer





























      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!






      share|improve this answer





























        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






        share|improve this answer






















          Your Answer





          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























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





          share|improve this answer
























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





            share|improve this answer






















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





              share|improve this answer












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






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 19 at 13:05









              Anton.P

              1,47031123




              1,47031123






















                  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.






                  share|improve this answer


























                    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.






                    share|improve this answer
























                      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.






                      share|improve this answer














                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Aug 19 at 11:18









                      zlakad

                      9171614




                      9171614










                      answered Aug 19 at 10:50









                      Sweeper

                      54.6k960117




                      54.6k960117




















                          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!






                          share|improve this answer


























                            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!






                            share|improve this answer
























                              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!






                              share|improve this answer














                              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!







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Aug 19 at 11:52

























                              answered Aug 19 at 11:33









                              Taslim

                              1,02921024




                              1,02921024




















                                  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






                                  share|improve this answer


























                                    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






                                    share|improve this answer
























                                      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






                                      share|improve this answer














                                      Please have a look at this solution, I little tested, it works.



                                      public static int number(int number, int divider) 


                                      p.s. little simplfied







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Aug 19 at 11:42

























                                      answered Aug 19 at 11:34









                                      ImZ

                                      266




                                      266



























                                           

                                          draft saved


                                          draft discarded















































                                           


                                          draft saved


                                          draft discarded














                                          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













































































                                          Comments

                                          Popular posts from this blog

                                          Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                          Confectionery