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

  • Home
  • SEARCH
  • 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 806793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:18:07+00:00 2026-05-15T00:18:07+00:00

I have an array with the ids of x cakes and another associative array

  • 0

I have an array with the ids of x cakes and another associative array with the ids of y people. I want to ensure that each cake is enjoyed by exactly 2 people and that each person gets a fair share of cake overall. However, cakes must be kept whole (i.e. if the average cake per person is a fraction, this fraction will be rounded up for some, and down for others). No person can be assigned the same cake twice. For example:

$cake = array(''1','2')
$people = array('1','2','3')

In order to do so, I wish to create a new array where each row represents a cake-person assignment. As each cake is being assigned to two people, the number of rows in this table should be exactly twice the number of cakes. There will not be exactly 1 solution to this problem but a solution to the above example would be:

$cake_person = array(
    '1'=>array('1', '1'),
    '2'=>array('1', '2'),
    '3'=>array('2', '2'),
    '4'=>array('2', '3'),
    )

Notice that people 1 and 3 are losing out but that is because there is no more cake to go around! Each cake must be given exactly twice.

How can I generate such a solution reliably for larger numbers of people and cakes?


Having implemented Artefacto’s useful response, I’ve decided to post my code below just in case anybody else finds it useful.

Data

//Create people array with 25 people
$people = range(1,25);

//Create cake array with 77 cakes
$cake = range(1,77);

Code

$people_cakes = array();
$totalPeople = count($people);
$idp = 0;

foreach($cakes as $cake) {
    $id1 = $idp % $totalPeople;
    $id2 = ($idp + 1) % $totalPeople;
    $people_cakes[] = array($people[$id + 1], $cake);
    $people_cakes[] = array($people[$id + 2], $cake);

    $idp = $idp + 2;
    }
  • 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-05-15T00:18:08+00:00Added an answer on May 15, 2026 at 12:18 am

    Implement an algorithm that iterates through the cakes and assigns parts in order:

    • Start with idp = 0
    • Iterate through the cakes
      • Give the first hald to person stored in position (idp mod total persons) of the persons’ array.
      • Give the second half to person stored in position ((idp+1) mod total persons) of the persons’ array.
      • Sum 2 to idp

    This will not give the same result as your example (person 1 will get two halves), but that was not a requirement.

    Example script:

    $cakes = range(1, 77);
    $people = range(1,25);
    
    $result = array();
    $idp = 0;
    foreach ($cakes as $cid) {
        $result[] = array(
            'cake_id' => $cid,
            'person_id' => $people[$idp % count($people)],
        );
        $result[] = array(
            'cake_id' => $cid,
            'person_id' => $people[($idp+1) % count($people)]
        );
        $idp += 2;
    }
    
    
    $total = array();
    foreach ($result as $a) {
        if (!array_key_exists($a['person_id'], $total)) {
            $total[$a['person_id']] = 0;
        }
        $total[$a['person_id']]++;
    }
    
    var_dump($total); //gives the number of halves per person
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of IDs that are already sorted in descending order as
I have an array full of random content item ids. I need to run
I have an array of numbers that potentially have up to 8 decimal places
I have an array of items that are time sensitive. After an amount of
I have an array of hashes, and I want the unique values out of
I have an array of shorts (short[]) that I need to write out to
When I have array of ids, like ids = [2,3,5] and I perform Comment.find(ids)
I have an array of ids like 127415157,31323794... (range not known). What is the
I have an array of IDs (e.g. array(1, 2, 10, 34, 100, 101) )
I have an array of employee ids: 34 , 35, 40, 67, 54 and

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.