Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7772709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:56:37+00:00 2026-06-01T16:56:37+00:00

I have a problem that comes up when I was developing an app on

  • 0

I have a problem that comes up when I was developing an app on Android. However, the problem is:

There are x boxes and y balls where x <= y, and I want to distribute the balls to put them inside the boxes in order. For example: 3 boxes; box A, box B and box C – and 5 balls; ball 1, ball 2, ball 3, ball 4, ball 5.

What I need is to put the first ball ball 1 inside box A, and ball 5 inside box C and the other balls are distributed between them all (does not matter if one box has more balls than the others). Here is a loop (missing an increment value) that simulates the problem:

int boxCount = 0; // first box is 0 and last box is x
int numOfBalls = y;
for(int i = 0; i < numOfBalls; i++, boxCount += ???)
{
    boxes.get(boxCount).add(balls.get(i));
}

What equation should I used instead of ??? to solve the problem?


EDIT:

Since x <= y, that means:

  • None of the boxes should be empty.
  • The difference between the boxes’ balls number should not be more than 1.

EDIT2

By in order, I meant this:

A   B   C
---------
1   3   5
2   4

not

A   B   C
---------
1   2   3
4   5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T16:56:37+00:00Added an answer on June 1, 2026 at 4:56 pm
    int flag;
    int lastBallAdded = 0;
    int k = numOfBalls/numOfBoxes;
    int m = numOfBalls%numOfBoxes;
    
    for(int i = 0; i < numOfBoxes; i++, lastBallAdded+=k+flag) {
        flag = i<m;
    
        for(int j=lastBallAdded;j<lastBallAdded + k + flag;j++) 
            boxes.get(i).add(balls.get(j));
    }
    

    This is the reasoning behind this solution:

    by the definition of the problem, the algorithm should put k= numOfBalls/numOfBoxes balls in each box, except for the firsts m = numOfBalls%numOfBoxes boxes, where you should put k+1 balls.

    You can alternatively write it as

    int i;
    for(i = 0; i < m; i++) {
        //add k+1 balls
    }
    
    for(;i<numOfBoxes; i++) {
        //add k balls
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing for Android and I have an app (called Forget-Me-Not) that uses a
I have a problem with an image preview that comes up when you hover
Hello there people of stack overflow, I have just started developing with android and
I'm in the process of developing an android game. I have an activity that
One problem that I come across regularly and yet don't have a solution to
A problem that has frequently come up in my career is I have some
I have encountered a problem that I have not come accross yet when setting
In a couple of scripts that I use I have problem that is intermittent.
I have a problem that confuses my users, being that although an item is
I have the problem that an specific step in Ant can only be executed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.