Contents

Sharing a Pizza 3 (with.Java)

   Jan 7, 2024     1 min read

This is a recap of the “Share a Pizza 3” problem.

We’re going to look at it as a coding test problem, provide a retrospective on how we solved it, and explore other ways to solve it.

Let’s start with the problem.

Problem

Mushy’s pizzeria cuts pizza into any number of slices, from two slices to ten slices.

Given the number of slices, slice, and the number of people eating the pizza, n, as parameters, complete the solution function to return how many slices of pizza must be ordered for n people to eat at least one slice.

Example input and output

slicenresult
7102
4123

My solution to the ### problem

class Solution {
    public int solution(int slice, int n) {
        int answer = 0;
        for(int i = 0; i < slice * n; i++){
            if(slice * i >= n){
                answer = i;
                break;
            }
        }
	} return answer;
}

solution description

int answer = 0;: Initialize the variable answer to store the resulting value.

for (int i = 0; i < slice _ n; i++) : Iterate through i from 0 to slice _ n.

if (slice * i >= n) : Checks if the current i multiplied by slice is greater than or equal to n.

answer = i;: If the condition is satisfied, store the current value of i in answer and end the loop.

break;: If the condition is satisfied, end the loop.

return answer;: Returns the calculated result, answer.

This code computes the quotient of dividing slice by n. It finds and returns the multiple of the first slice that is greater than or equal to n.