Minimum Power Consumption (Juice Machines)

Quick Overview

Find the minimum total power needed to serve exactly a given number of customers with two machines that serve 100 and 4 customers per run, returning -1 when it cannot be done. It tests divisibility reasoning, bounded enumeration over one variable, and care with integer overflow.

Minimum Power Consumption (Juice Machines)

Company: Tech Mahindra

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

A juice shop operates two machines. Machine M serves exactly `100` customers per run and consumes `x` power units per run. Machine N serves exactly `4` customers per run and consumes `y` power units per run. Given the total number of customers `p` to be served, and the per-run power costs `x` and `y`, return the minimum total power units required to serve exactly `p` customers using any non-negative integer combination of runs from the two machines. If it is impossible to serve exactly `p` customers, return `-1`. ### Function Signature ```python def min_power_consumption(p: int, x: int, y: int) -> int: ``` ### Rules - Each run serves exactly its machine's capacity. Each machine can run any whole number of times, including zero. - The customers served must add up to exactly `p`. Serving more than `p` is not allowed. - If `p = 0`, no runs are needed and the answer is `0`. ### Constraints - `0 <= p <= 10^6` - `1 <= x <= 10^4` - `1 <= y <= 10^4` ### Examples ```text Input: p = 200, x = 10, y = 1 Output: 20 ``` Two runs of M serve 200 customers for 20 units. One M run plus 25 N runs costs 35, and 50 N runs cost 50, so 20 is the minimum. ```text Input: p = 108, x = 5, y = 2 Output: 9 ``` One M run and two N runs serve 100 + 8 = 108 customers for 5 + 4 = 9 units. Using only Machine N takes 27 runs and costs 54. ```text Input: p = 250, x = 3, y = 1 Output: -1 ``` Both capacities are multiples of 4, so every combination serves a multiple of 4 customers, and 250 is not a multiple of 4.

Overview: Find the minimum total power needed to serve exactly a given number of customers with two machines that serve 100 and 4 customers per run, returning -1 when it cannot be done. It tests divisibility reasoning, bounded enumeration over one variable, and care with integer overflow.

Tech Mahindra logo
Tech Mahindra
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

A juice shop operates two machines. Machine M serves exactly 100 customers per run and consumes x power units per run. Machine N serves exactly 4 customers per run and consumes y power units per run.

Given the total number of customers p to be served, and the per-run power costs x and y, return the minimum total power units required to serve exactly p customers using any non-negative integer combination of runs from the two machines.

If it is impossible to serve exactly p customers, return -1.

Function Signature

def min_power_consumption(p: int, x: int, y: int) -> int:

Rules

  • Each run serves exactly its machine's capacity. Each machine can run any whole number of times, including zero.
  • The customers served must add up to exactly p . Serving more than p is not allowed.
  • If p = 0 , no runs are needed and the answer is 0 .

Constraints

  • 0 <= p <= 10^6
  • 1 <= x <= 10^4
  • 1 <= y <= 10^4

Examples

Input: p = 200, x = 10, y = 1
Output: 20

Two runs of M serve 200 customers for 20 units. One M run plus 25 N runs costs 35, and 50 N runs cost 50, so 20 is the minimum.

Input: p = 108, x = 5, y = 2
Output: 9

One M run and two N runs serve 100 + 8 = 108 customers for 5 + 4 = 9 units. Using only Machine N takes 27 runs and costs 54.

Input: p = 250, x = 3, y = 1
Output: -1

Both capacities are multiples of 4, so every combination serves a multiple of 4 customers, and 250 is not a multiple of 4.